libavformat/mpeg.c
Go to the documentation of this file.
00001 /*
00002  * MPEG1/2 demuxer
00003  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "avformat.h"
00023 #include "internal.h"
00024 #include "mpeg.h"
00025 
00026 #undef NDEBUG
00027 #include <assert.h>
00028 
00029 /*********************************************/
00030 /* demux code */
00031 
00032 #define MAX_SYNC_SIZE 100000
00033 
00034 static int check_pes(uint8_t *p, uint8_t *end){
00035     int pes1;
00036     int pes2=      (p[3] & 0xC0) == 0x80
00037                 && (p[4] & 0xC0) != 0x40
00038                 &&((p[4] & 0xC0) == 0x00 || (p[4]&0xC0)>>2 == (p[6]&0xF0));
00039 
00040     for(p+=3; p<end && *p == 0xFF; p++);
00041     if((*p&0xC0) == 0x40) p+=2;
00042     if((*p&0xF0) == 0x20){
00043         pes1= p[0]&p[2]&p[4]&1;
00044     }else if((*p&0xF0) == 0x30){
00045         pes1= p[0]&p[2]&p[4]&p[5]&p[7]&p[9]&1;
00046     }else
00047         pes1 = *p == 0x0F;
00048 
00049     return pes1||pes2;
00050 }
00051 
00052 static int check_pack_header(const uint8_t *buf) {
00053     return (buf[1] & 0xC0) == 0x40 || (buf[1] & 0xF0) == 0x20;
00054 }
00055 
00056 static int mpegps_probe(AVProbeData *p)
00057 {
00058     uint32_t code= -1;
00059     int sys=0, pspack=0, priv1=0, vid=0, audio=0, invalid=0;
00060     int i;
00061     int score=0;
00062 
00063     for(i=0; i<p->buf_size; i++){
00064         code = (code<<8) + p->buf[i];
00065         if ((code & 0xffffff00) == 0x100) {
00066             int len= p->buf[i+1] << 8 | p->buf[i+2];
00067             int pes= check_pes(p->buf+i, p->buf+p->buf_size);
00068             int pack = check_pack_header(p->buf+i);
00069 
00070             if(code == SYSTEM_HEADER_START_CODE) sys++;
00071             else if(code == PACK_START_CODE && pack) pspack++;
00072             else if((code & 0xf0) == VIDEO_ID &&  pes) vid++;
00073             // skip pes payload to avoid start code emulation for private
00074             // and audio streams
00075             else if((code & 0xe0) == AUDIO_ID &&  pes) {audio++; i+=len;}
00076             else if(code == PRIVATE_STREAM_1  &&  pes) {priv1++; i+=len;}
00077 
00078             else if((code & 0xf0) == VIDEO_ID && !pes) invalid++;
00079             else if((code & 0xe0) == AUDIO_ID && !pes) invalid++;
00080             else if(code == PRIVATE_STREAM_1  && !pes) invalid++;
00081         }
00082     }
00083 
00084     if(vid+audio > invalid)     /* invalid VDR files nd short PES streams */
00085         score= AVPROBE_SCORE_MAX/4;
00086 
00087 //av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d %d len:%d\n", sys, priv1, pspack,vid, audio, invalid, p->buf_size);
00088     if(sys>invalid && sys*9 <= pspack*10)
00089         return pspack > 2 ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4; // +1 for .mpg
00090     if(pspack > invalid && (priv1+vid+audio)*10 >= pspack*9)
00091         return pspack > 2 ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4; // +1 for .mpg
00092     if((!!vid ^ !!audio) && (audio > 4 || vid > 1) && !sys && !pspack && p->buf_size>2048 && vid + audio > invalid) /* PES stream */
00093         return (audio > 12 || vid > 3) ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4;
00094 
00095     //02-Penguin.flac has sys:0 priv1:0 pspack:0 vid:0 audio:1
00096     //mp3_misidentified_2.mp3 has sys:0 priv1:0 pspack:0 vid:0 audio:6
00097     return score;
00098 }
00099 
00100 
00101 typedef struct MpegDemuxContext {
00102     int32_t header_state;
00103     unsigned char psm_es_type[256];
00104     int sofdec;
00105 } MpegDemuxContext;
00106 
00107 static int mpegps_read_header(AVFormatContext *s,
00108                               AVFormatParameters *ap)
00109 {
00110     MpegDemuxContext *m = s->priv_data;
00111     const char *sofdec = "Sofdec";
00112     int v, i = 0;
00113 
00114     m->header_state = 0xff;
00115     s->ctx_flags |= AVFMTCTX_NOHEADER;
00116 
00117     m->sofdec = -1;
00118     do {
00119         v = avio_r8(s->pb);
00120         m->header_state = m->header_state << 8 | v;
00121         m->sofdec++;
00122     } while (v == sofdec[i] && i++ < 6);
00123 
00124     m->sofdec = (m->sofdec == 6) ? 1 : 0;
00125 
00126     /* no need to do more */
00127     return 0;
00128 }
00129 
00130 static int64_t get_pts(AVIOContext *pb, int c)
00131 {
00132     uint8_t buf[5];
00133 
00134     buf[0] = c<0 ? avio_r8(pb) : c;
00135     avio_read(pb, buf+1, 4);
00136 
00137     return ff_parse_pes_pts(buf);
00138 }
00139 
00140 static int find_next_start_code(AVIOContext *pb, int *size_ptr,
00141                                 int32_t *header_state)
00142 {
00143     unsigned int state, v;
00144     int val, n;
00145 
00146     state = *header_state;
00147     n = *size_ptr;
00148     while (n > 0) {
00149         if (pb->eof_reached)
00150             break;
00151         v = avio_r8(pb);
00152         n--;
00153         if (state == 0x000001) {
00154             state = ((state << 8) | v) & 0xffffff;
00155             val = state;
00156             goto found;
00157         }
00158         state = ((state << 8) | v) & 0xffffff;
00159     }
00160     val = -1;
00161  found:
00162     *header_state = state;
00163     *size_ptr = n;
00164     return val;
00165 }
00166 
00167 #if 0 /* unused, remove? */
00168 /* XXX: optimize */
00169 static int find_prev_start_code(AVIOContext *pb, int *size_ptr)
00170 {
00171     int64_t pos, pos_start;
00172     int max_size, start_code;
00173 
00174     max_size = *size_ptr;
00175     pos_start = avio_tell(pb);
00176 
00177     /* in order to go faster, we fill the buffer */
00178     pos = pos_start - 16386;
00179     if (pos < 0)
00180         pos = 0;
00181     avio_seek(pb, pos, SEEK_SET);
00182     avio_r8(pb);
00183 
00184     pos = pos_start;
00185     for(;;) {
00186         pos--;
00187         if (pos < 0 || (pos_start - pos) >= max_size) {
00188             start_code = -1;
00189             goto the_end;
00190         }
00191         avio_seek(pb, pos, SEEK_SET);
00192         start_code = avio_rb32(pb);
00193         if ((start_code & 0xffffff00) == 0x100)
00194             break;
00195     }
00196  the_end:
00197     *size_ptr = pos_start - pos;
00198     return start_code;
00199 }
00200 #endif
00201 
00208 static long mpegps_psm_parse(MpegDemuxContext *m, AVIOContext *pb)
00209 {
00210     int psm_length, ps_info_length, es_map_length;
00211 
00212     psm_length = avio_rb16(pb);
00213     avio_r8(pb);
00214     avio_r8(pb);
00215     ps_info_length = avio_rb16(pb);
00216 
00217     /* skip program_stream_info */
00218     avio_skip(pb, ps_info_length);
00219     es_map_length = avio_rb16(pb);
00220 
00221     /* at least one es available? */
00222     while (es_map_length >= 4){
00223         unsigned char type      = avio_r8(pb);
00224         unsigned char es_id     = avio_r8(pb);
00225         uint16_t es_info_length = avio_rb16(pb);
00226         /* remember mapping from stream id to stream type */
00227         m->psm_es_type[es_id] = type;
00228         /* skip program_stream_info */
00229         avio_skip(pb, es_info_length);
00230         es_map_length -= 4 + es_info_length;
00231     }
00232     avio_rb32(pb); /* crc32 */
00233     return 2 + psm_length;
00234 }
00235 
00236 /* read the next PES header. Return its position in ppos
00237    (if not NULL), and its start code, pts and dts.
00238  */
00239 static int mpegps_read_pes_header(AVFormatContext *s,
00240                                   int64_t *ppos, int *pstart_code,
00241                                   int64_t *ppts, int64_t *pdts)
00242 {
00243     MpegDemuxContext *m = s->priv_data;
00244     int len, size, startcode, c, flags, header_len;
00245     int pes_ext, ext2_len, id_ext, skip;
00246     int64_t pts, dts;
00247     int64_t last_sync= avio_tell(s->pb);
00248 
00249  error_redo:
00250         avio_seek(s->pb, last_sync, SEEK_SET);
00251  redo:
00252         /* next start code (should be immediately after) */
00253         m->header_state = 0xff;
00254         size = MAX_SYNC_SIZE;
00255         startcode = find_next_start_code(s->pb, &size, &m->header_state);
00256         last_sync = avio_tell(s->pb);
00257     //printf("startcode=%x pos=0x%"PRIx64"\n", startcode, avio_tell(s->pb));
00258     if (startcode < 0){
00259         if(s->pb->eof_reached)
00260             return AVERROR_EOF;
00261         //FIXME we should remember header_state
00262         return AVERROR(EAGAIN);
00263     }
00264 
00265     if (startcode == PACK_START_CODE)
00266         goto redo;
00267     if (startcode == SYSTEM_HEADER_START_CODE)
00268         goto redo;
00269     if (startcode == PADDING_STREAM) {
00270         avio_skip(s->pb, avio_rb16(s->pb));
00271         goto redo;
00272     }
00273     if (startcode == PRIVATE_STREAM_2) {
00274         len = avio_rb16(s->pb);
00275         if (!m->sofdec) {
00276             while (len-- >= 6) {
00277                 if (avio_r8(s->pb) == 'S') {
00278                     uint8_t buf[5];
00279                     avio_read(s->pb, buf, sizeof(buf));
00280                     m->sofdec = !memcmp(buf, "ofdec", 5);
00281                     len -= sizeof(buf);
00282                     break;
00283                 }
00284             }
00285             m->sofdec -= !m->sofdec;
00286         }
00287         avio_skip(s->pb, len);
00288         goto redo;
00289     }
00290     if (startcode == PROGRAM_STREAM_MAP) {
00291         mpegps_psm_parse(m, s->pb);
00292         goto redo;
00293     }
00294 
00295     /* find matching stream */
00296     if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
00297           (startcode >= 0x1e0 && startcode <= 0x1ef) ||
00298           (startcode == 0x1bd) || (startcode == 0x1fd)))
00299         goto redo;
00300     if (ppos) {
00301         *ppos = avio_tell(s->pb) - 4;
00302     }
00303     len = avio_rb16(s->pb);
00304     pts =
00305     dts = AV_NOPTS_VALUE;
00306     /* stuffing */
00307     for(;;) {
00308         if (len < 1)
00309             goto error_redo;
00310         c = avio_r8(s->pb);
00311         len--;
00312         /* XXX: for mpeg1, should test only bit 7 */
00313         if (c != 0xff)
00314             break;
00315     }
00316     if ((c & 0xc0) == 0x40) {
00317         /* buffer scale & size */
00318         avio_r8(s->pb);
00319         c = avio_r8(s->pb);
00320         len -= 2;
00321     }
00322     if ((c & 0xe0) == 0x20) {
00323         dts = pts = get_pts(s->pb, c);
00324         len -= 4;
00325         if (c & 0x10){
00326             dts = get_pts(s->pb, -1);
00327             len -= 5;
00328         }
00329     } else if ((c & 0xc0) == 0x80) {
00330         /* mpeg 2 PES */
00331 #if 0 /* some streams have this field set for no apparent reason */
00332         if ((c & 0x30) != 0) {
00333             /* Encrypted multiplex not handled */
00334             goto redo;
00335         }
00336 #endif
00337         flags = avio_r8(s->pb);
00338         header_len = avio_r8(s->pb);
00339         len -= 2;
00340         if (header_len > len)
00341             goto error_redo;
00342         len -= header_len;
00343         if (flags & 0x80) {
00344             dts = pts = get_pts(s->pb, -1);
00345             header_len -= 5;
00346             if (flags & 0x40) {
00347                 dts = get_pts(s->pb, -1);
00348                 header_len -= 5;
00349             }
00350         }
00351         if (flags & 0x3f && header_len == 0){
00352             flags &= 0xC0;
00353             av_log(s, AV_LOG_WARNING, "Further flags set but no bytes left\n");
00354         }
00355         if (flags & 0x01) { /* PES extension */
00356             pes_ext = avio_r8(s->pb);
00357             header_len--;
00358             /* Skip PES private data, program packet sequence counter and P-STD buffer */
00359             skip = (pes_ext >> 4) & 0xb;
00360             skip += skip & 0x9;
00361             if (pes_ext & 0x40 || skip > header_len){
00362                 av_log(s, AV_LOG_WARNING, "pes_ext %X is invalid\n", pes_ext);
00363                 pes_ext=skip=0;
00364             }
00365             avio_skip(s->pb, skip);
00366             header_len -= skip;
00367 
00368             if (pes_ext & 0x01) { /* PES extension 2 */
00369                 ext2_len = avio_r8(s->pb);
00370                 header_len--;
00371                 if ((ext2_len & 0x7f) > 0) {
00372                     id_ext = avio_r8(s->pb);
00373                     if ((id_ext & 0x80) == 0)
00374                         startcode = ((startcode & 0xff) << 8) | id_ext;
00375                     header_len--;
00376                 }
00377             }
00378         }
00379         if(header_len < 0)
00380             goto error_redo;
00381         avio_skip(s->pb, header_len);
00382     }
00383     else if( c!= 0xf )
00384         goto redo;
00385 
00386     if (startcode == PRIVATE_STREAM_1 && !m->psm_es_type[startcode & 0xff]) {
00387         startcode = avio_r8(s->pb);
00388         len--;
00389         if (startcode >= 0x80 && startcode <= 0xcf) {
00390             /* audio: skip header */
00391             avio_r8(s->pb);
00392             avio_r8(s->pb);
00393             avio_r8(s->pb);
00394             len -= 3;
00395             if (startcode >= 0xb0 && startcode <= 0xbf) {
00396                 /* MLP/TrueHD audio has a 4-byte header */
00397                 avio_r8(s->pb);
00398                 len--;
00399             }
00400         }
00401     }
00402     if(len<0)
00403         goto error_redo;
00404     if(dts != AV_NOPTS_VALUE && ppos){
00405         int i;
00406         for(i=0; i<s->nb_streams; i++){
00407             if(startcode == s->streams[i]->id &&
00408                s->pb->seekable /* index useless on streams anyway */) {
00409                 ff_reduce_index(s, i);
00410                 av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
00411             }
00412         }
00413     }
00414 
00415     *pstart_code = startcode;
00416     *ppts = pts;
00417     *pdts = dts;
00418     return len;
00419 }
00420 
00421 static int mpegps_read_packet(AVFormatContext *s,
00422                               AVPacket *pkt)
00423 {
00424     MpegDemuxContext *m = s->priv_data;
00425     AVStream *st;
00426     int len, startcode, i, es_type, ret;
00427     enum CodecID codec_id = CODEC_ID_NONE;
00428     enum AVMediaType type;
00429     int64_t pts, dts, dummy_pos; //dummy_pos is needed for the index building to work
00430     uint8_t av_uninit(dvdaudio_substream_type);
00431 
00432  redo:
00433     len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts);
00434     if (len < 0)
00435         return len;
00436 
00437     if(startcode == 0x1bd) {
00438         dvdaudio_substream_type = avio_r8(s->pb);
00439         avio_skip(s->pb, 3);
00440         len -= 4;
00441     }
00442 
00443     /* now find stream */
00444     for(i=0;i<s->nb_streams;i++) {
00445         st = s->streams[i];
00446         if (st->id == startcode)
00447             goto found;
00448     }
00449 
00450     es_type = m->psm_es_type[startcode & 0xff];
00451     if(es_type > 0 && es_type != STREAM_TYPE_PRIVATE_DATA){
00452         if(es_type == STREAM_TYPE_VIDEO_MPEG1){
00453             codec_id = CODEC_ID_MPEG2VIDEO;
00454             type = AVMEDIA_TYPE_VIDEO;
00455         } else if(es_type == STREAM_TYPE_VIDEO_MPEG2){
00456             codec_id = CODEC_ID_MPEG2VIDEO;
00457             type = AVMEDIA_TYPE_VIDEO;
00458         } else if(es_type == STREAM_TYPE_AUDIO_MPEG1 ||
00459                   es_type == STREAM_TYPE_AUDIO_MPEG2){
00460             codec_id = CODEC_ID_MP3;
00461             type = AVMEDIA_TYPE_AUDIO;
00462         } else if(es_type == STREAM_TYPE_AUDIO_AAC){
00463             codec_id = CODEC_ID_AAC;
00464             type = AVMEDIA_TYPE_AUDIO;
00465         } else if(es_type == STREAM_TYPE_VIDEO_MPEG4){
00466             codec_id = CODEC_ID_MPEG4;
00467             type = AVMEDIA_TYPE_VIDEO;
00468         } else if(es_type == STREAM_TYPE_VIDEO_H264){
00469             codec_id = CODEC_ID_H264;
00470             type = AVMEDIA_TYPE_VIDEO;
00471         } else if(es_type == STREAM_TYPE_AUDIO_AC3){
00472             codec_id = CODEC_ID_AC3;
00473             type = AVMEDIA_TYPE_AUDIO;
00474         } else {
00475             goto skip;
00476         }
00477     } else if (startcode >= 0x1e0 && startcode <= 0x1ef) {
00478         static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
00479         unsigned char buf[8];
00480         avio_read(s->pb, buf, 8);
00481         avio_seek(s->pb, -8, SEEK_CUR);
00482         if(!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1))
00483             codec_id = CODEC_ID_CAVS;
00484         else
00485             codec_id = CODEC_ID_PROBE;
00486         type = AVMEDIA_TYPE_VIDEO;
00487     } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
00488         type = AVMEDIA_TYPE_AUDIO;
00489         codec_id = m->sofdec > 0 ? CODEC_ID_ADPCM_ADX : CODEC_ID_MP2;
00490     } else if (startcode >= 0x80 && startcode <= 0x87) {
00491         type = AVMEDIA_TYPE_AUDIO;
00492         codec_id = CODEC_ID_AC3;
00493     } else if (  ( startcode >= 0x88 && startcode <= 0x8f)
00494                ||( startcode >= 0x98 && startcode <= 0x9f)) {
00495         /* 0x90 - 0x97 is reserved for SDDS in DVD specs */
00496         type = AVMEDIA_TYPE_AUDIO;
00497         codec_id = CODEC_ID_DTS;
00498     } else if (startcode >= 0xa0 && startcode <= 0xaf) {
00499         type = AVMEDIA_TYPE_AUDIO;
00500         /* 16 bit form will be handled as CODEC_ID_PCM_S16BE */
00501         codec_id = CODEC_ID_PCM_DVD;
00502     } else if (startcode >= 0xb0 && startcode <= 0xbf) {
00503         type = AVMEDIA_TYPE_AUDIO;
00504         codec_id = CODEC_ID_TRUEHD;
00505     } else if (startcode >= 0xc0 && startcode <= 0xcf) {
00506         /* Used for both AC-3 and E-AC-3 in EVOB files */
00507         type = AVMEDIA_TYPE_AUDIO;
00508         codec_id = CODEC_ID_AC3;
00509     } else if (startcode >= 0x20 && startcode <= 0x3f) {
00510         type = AVMEDIA_TYPE_SUBTITLE;
00511         codec_id = CODEC_ID_DVD_SUBTITLE;
00512     } else if (startcode >= 0xfd55 && startcode <= 0xfd5f) {
00513         type = AVMEDIA_TYPE_VIDEO;
00514         codec_id = CODEC_ID_VC1;
00515     } else if (startcode == 0x1bd) {
00516         // check dvd audio substream type
00517         type = AVMEDIA_TYPE_AUDIO;
00518         switch(dvdaudio_substream_type & 0xe0) {
00519         case 0xa0:  codec_id = CODEC_ID_PCM_DVD;
00520                     break;
00521         case 0x80:  if((dvdaudio_substream_type & 0xf8) == 0x88)
00522                          codec_id = CODEC_ID_DTS;
00523                     else codec_id = CODEC_ID_AC3;
00524                     break;
00525         default:    av_log(s, AV_LOG_ERROR, "Unknown 0x1bd sub-stream\n");
00526                     goto skip;
00527         }
00528     } else {
00529     skip:
00530         /* skip packet */
00531         avio_skip(s->pb, len);
00532         goto redo;
00533     }
00534     /* no stream found: add a new stream */
00535     st = avformat_new_stream(s, NULL);
00536     if (!st)
00537         goto skip;
00538     st->id = startcode;
00539     st->codec->codec_type = type;
00540     st->codec->codec_id = codec_id;
00541     if (codec_id != CODEC_ID_PCM_S16BE)
00542         st->need_parsing = AVSTREAM_PARSE_FULL;
00543  found:
00544     if(st->discard >= AVDISCARD_ALL)
00545         goto skip;
00546     if ((startcode >= 0xa0 && startcode <= 0xaf) ||
00547         (startcode == 0x1bd && ((dvdaudio_substream_type & 0xe0) == 0xa0))) {
00548         int b1, freq;
00549 
00550         /* for LPCM, we just skip the header and consider it is raw
00551            audio data */
00552         if (len <= 3)
00553             goto skip;
00554         avio_r8(s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
00555         b1 = avio_r8(s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
00556         avio_r8(s->pb); /* dynamic range control (0x80 = off) */
00557         len -= 3;
00558         freq = (b1 >> 4) & 3;
00559         st->codec->sample_rate = lpcm_freq_tab[freq];
00560         st->codec->channels = 1 + (b1 & 7);
00561         st->codec->bits_per_coded_sample = 16 + ((b1 >> 6) & 3) * 4;
00562         st->codec->bit_rate = st->codec->channels *
00563                               st->codec->sample_rate *
00564                               st->codec->bits_per_coded_sample;
00565         if (st->codec->bits_per_coded_sample == 16)
00566             st->codec->codec_id = CODEC_ID_PCM_S16BE;
00567         else if (st->codec->bits_per_coded_sample == 28)
00568             return AVERROR(EINVAL);
00569     }
00570     ret = av_get_packet(s->pb, pkt, len);
00571     pkt->pts = pts;
00572     pkt->dts = dts;
00573     pkt->pos = dummy_pos;
00574     pkt->stream_index = st->index;
00575     av_dlog(s, "%d: pts=%0.3f dts=%0.3f size=%d\n",
00576             pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0,
00577             pkt->size);
00578 
00579     return (ret < 0) ? ret : 0;
00580 }
00581 
00582 static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
00583                                int64_t *ppos, int64_t pos_limit)
00584 {
00585     int len, startcode;
00586     int64_t pos, pts, dts;
00587 
00588     pos = *ppos;
00589     if (avio_seek(s->pb, pos, SEEK_SET) < 0)
00590         return AV_NOPTS_VALUE;
00591 
00592     for(;;) {
00593         len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts);
00594         if (len < 0) {
00595             av_dlog(s, "none (ret=%d)\n", len);
00596             return AV_NOPTS_VALUE;
00597         }
00598         if (startcode == s->streams[stream_index]->id &&
00599             dts != AV_NOPTS_VALUE) {
00600             break;
00601         }
00602         avio_skip(s->pb, len);
00603     }
00604     av_dlog(s, "pos=0x%"PRIx64" dts=0x%"PRIx64" %0.3f\n",
00605             pos, dts, dts / 90000.0);
00606     *ppos = pos;
00607     return dts;
00608 }
00609 
00610 AVInputFormat ff_mpegps_demuxer = {
00611     .name           = "mpeg",
00612     .long_name      = NULL_IF_CONFIG_SMALL("MPEG-PS format"),
00613     .priv_data_size = sizeof(MpegDemuxContext),
00614     .read_probe     = mpegps_probe,
00615     .read_header    = mpegps_read_header,
00616     .read_packet    = mpegps_read_packet,
00617     .read_timestamp = mpegps_read_dts,
00618     .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
00619 };