1/*
2 * MPEG1/2 demuxer
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 *
5 * This file is part of Libav.
6 *
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "avformat.h"
23#include "internal.h"
24#include "mpeg.h"
25
26#undef NDEBUG
27#include <assert.h>
28
29/*********************************************/
30/* demux code */
31
32#define MAX_SYNC_SIZE 100000
33
34static int check_pes(uint8_t *p, uint8_t *end){
35    int pes1;
36    int pes2=      (p[3] & 0xC0) == 0x80
37                && (p[4] & 0xC0) != 0x40
38                &&((p[4] & 0xC0) == 0x00 || (p[4]&0xC0)>>2 == (p[6]&0xF0));
39
40    for(p+=3; p<end && *p == 0xFF; p++);
41    if((*p&0xC0) == 0x40) p+=2;
42    if((*p&0xF0) == 0x20){
43        pes1= p[0]&p[2]&p[4]&1;
44    }else if((*p&0xF0) == 0x30){
45        pes1= p[0]&p[2]&p[4]&p[5]&p[7]&p[9]&1;
46    }else
47        pes1 = *p == 0x0F;
48
49    return pes1||pes2;
50}
51
52static int check_pack_header(const uint8_t *buf) {
53    return (buf[1] & 0xC0) == 0x40 || (buf[1] & 0xF0) == 0x20;
54}
55
56static int mpegps_probe(AVProbeData *p)
57{
58    uint32_t code= -1;
59    int sys=0, pspack=0, priv1=0, vid=0, audio=0, invalid=0;
60    int i;
61    int score=0;
62
63    for(i=0; i<p->buf_size; i++){
64        code = (code<<8) + p->buf[i];
65        if ((code & 0xffffff00) == 0x100) {
66            int len= p->buf[i+1] << 8 | p->buf[i+2];
67            int pes= check_pes(p->buf+i, p->buf+p->buf_size);
68            int pack = check_pack_header(p->buf+i);
69
70            if(code == SYSTEM_HEADER_START_CODE) sys++;
71            else if(code == PACK_START_CODE && pack) pspack++;
72            else if((code & 0xf0) == VIDEO_ID &&  pes) vid++;
73            // skip pes payload to avoid start code emulation for private
74            // and audio streams
75            else if((code & 0xe0) == AUDIO_ID &&  pes) {audio++; i+=len;}
76            else if(code == PRIVATE_STREAM_1  &&  pes) {priv1++; i+=len;}
77
78            else if((code & 0xf0) == VIDEO_ID && !pes) invalid++;
79            else if((code & 0xe0) == AUDIO_ID && !pes) invalid++;
80            else if(code == PRIVATE_STREAM_1  && !pes) invalid++;
81        }
82    }
83
84    if(vid+audio > invalid)     /* invalid VDR files nd short PES streams */
85        score= AVPROBE_SCORE_MAX/4;
86
87//av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d %d len:%d\n", sys, priv1, pspack,vid, audio, invalid, p->buf_size);
88    if(sys>invalid && sys*9 <= pspack*10)
89        return pspack > 2 ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4; // +1 for .mpg
90    if(pspack > invalid && (priv1+vid+audio)*10 >= pspack*9)
91        return pspack > 2 ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4; // +1 for .mpg
92    if((!!vid ^ !!audio) && (audio > 4 || vid > 1) && !sys && !pspack && p->buf_size>2048 && vid + audio > invalid) /* PES stream */
93        return (audio > 12 || vid > 3) ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4;
94
95    //02-Penguin.flac has sys:0 priv1:0 pspack:0 vid:0 audio:1
96    //mp3_misidentified_2.mp3 has sys:0 priv1:0 pspack:0 vid:0 audio:6
97    return score;
98}
99
100
101typedef struct MpegDemuxContext {
102    int32_t header_state;
103    unsigned char psm_es_type[256];
104    int sofdec;
105} MpegDemuxContext;
106
107static int mpegps_read_header(AVFormatContext *s,
108                              AVFormatParameters *ap)
109{
110    MpegDemuxContext *m = s->priv_data;
111    const char *sofdec = "Sofdec";
112    int v, i = 0;
113
114    m->header_state = 0xff;
115    s->ctx_flags |= AVFMTCTX_NOHEADER;
116
117    m->sofdec = -1;
118    do {
119        v = avio_r8(s->pb);
120        m->header_state = m->header_state << 8 | v;
121        m->sofdec++;
122    } while (v == sofdec[i] && i++ < 6);
123
124    m->sofdec = (m->sofdec == 6) ? 1 : 0;
125
126    /* no need to do more */
127    return 0;
128}
129
130static int64_t get_pts(AVIOContext *pb, int c)
131{
132    uint8_t buf[5];
133
134    buf[0] = c<0 ? avio_r8(pb) : c;
135    avio_read(pb, buf+1, 4);
136
137    return ff_parse_pes_pts(buf);
138}
139
140static int find_next_start_code(AVIOContext *pb, int *size_ptr,
141                                int32_t *header_state)
142{
143    unsigned int state, v;
144    int val, n;
145
146    state = *header_state;
147    n = *size_ptr;
148    while (n > 0) {
149        if (pb->eof_reached)
150            break;
151        v = avio_r8(pb);
152        n--;
153        if (state == 0x000001) {
154            state = ((state << 8) | v) & 0xffffff;
155            val = state;
156            goto found;
157        }
158        state = ((state << 8) | v) & 0xffffff;
159    }
160    val = -1;
161 found:
162    *header_state = state;
163    *size_ptr = n;
164    return val;
165}
166
167#if 0 /* unused, remove? */
168/* XXX: optimize */
169static int find_prev_start_code(AVIOContext *pb, int *size_ptr)
170{
171    int64_t pos, pos_start;
172    int max_size, start_code;
173
174    max_size = *size_ptr;
175    pos_start = avio_tell(pb);
176
177    /* in order to go faster, we fill the buffer */
178    pos = pos_start - 16386;
179    if (pos < 0)
180        pos = 0;
181    avio_seek(pb, pos, SEEK_SET);
182    avio_r8(pb);
183
184    pos = pos_start;
185    for(;;) {
186        pos--;
187        if (pos < 0 || (pos_start - pos) >= max_size) {
188            start_code = -1;
189            goto the_end;
190        }
191        avio_seek(pb, pos, SEEK_SET);
192        start_code = avio_rb32(pb);
193        if ((start_code & 0xffffff00) == 0x100)
194            break;
195    }
196 the_end:
197    *size_ptr = pos_start - pos;
198    return start_code;
199}
200#endif
201
202/**
203 * Extract stream types from a program stream map
204 * According to ISO/IEC 13818-1 ('MPEG-2 Systems') table 2-35
205 *
206 * @return number of bytes occupied by PSM in the bitstream
207 */
208static long mpegps_psm_parse(MpegDemuxContext *m, AVIOContext *pb)
209{
210    int psm_length, ps_info_length, es_map_length;
211
212    psm_length = avio_rb16(pb);
213    avio_r8(pb);
214    avio_r8(pb);
215    ps_info_length = avio_rb16(pb);
216
217    /* skip program_stream_info */
218    avio_skip(pb, ps_info_length);
219    es_map_length = avio_rb16(pb);
220
221    /* at least one es available? */
222    while (es_map_length >= 4){
223        unsigned char type      = avio_r8(pb);
224        unsigned char es_id     = avio_r8(pb);
225        uint16_t es_info_length = avio_rb16(pb);
226        /* remember mapping from stream id to stream type */
227        m->psm_es_type[es_id] = type;
228        /* skip program_stream_info */
229        avio_skip(pb, es_info_length);
230        es_map_length -= 4 + es_info_length;
231    }
232    avio_rb32(pb); /* crc32 */
233    return 2 + psm_length;
234}
235
236/* read the next PES header. Return its position in ppos
237   (if not NULL), and its start code, pts and dts.
238 */
239static int mpegps_read_pes_header(AVFormatContext *s,
240                                  int64_t *ppos, int *pstart_code,
241                                  int64_t *ppts, int64_t *pdts)
242{
243    MpegDemuxContext *m = s->priv_data;
244    int len, size, startcode, c, flags, header_len;
245    int pes_ext, ext2_len, id_ext, skip;
246    int64_t pts, dts;
247    int64_t last_sync= avio_tell(s->pb);
248
249 error_redo:
250        avio_seek(s->pb, last_sync, SEEK_SET);
251 redo:
252        /* next start code (should be immediately after) */
253        m->header_state = 0xff;
254        size = MAX_SYNC_SIZE;
255        startcode = find_next_start_code(s->pb, &size, &m->header_state);
256        last_sync = avio_tell(s->pb);
257    //printf("startcode=%x pos=0x%"PRIx64"\n", startcode, avio_tell(s->pb));
258    if (startcode < 0){
259        if(s->pb->eof_reached)
260            return AVERROR_EOF;
261        //FIXME we should remember header_state
262        return AVERROR(EAGAIN);
263    }
264
265    if (startcode == PACK_START_CODE)
266        goto redo;
267    if (startcode == SYSTEM_HEADER_START_CODE)
268        goto redo;
269    if (startcode == PADDING_STREAM) {
270        avio_skip(s->pb, avio_rb16(s->pb));
271        goto redo;
272    }
273    if (startcode == PRIVATE_STREAM_2) {
274        len = avio_rb16(s->pb);
275        if (!m->sofdec) {
276            while (len-- >= 6) {
277                if (avio_r8(s->pb) == 'S') {
278                    uint8_t buf[5];
279                    avio_read(s->pb, buf, sizeof(buf));
280                    m->sofdec = !memcmp(buf, "ofdec", 5);
281                    len -= sizeof(buf);
282                    break;
283                }
284            }
285            m->sofdec -= !m->sofdec;
286        }
287        avio_skip(s->pb, len);
288        goto redo;
289    }
290    if (startcode == PROGRAM_STREAM_MAP) {
291        mpegps_psm_parse(m, s->pb);
292        goto redo;
293    }
294
295    /* find matching stream */
296    if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
297          (startcode >= 0x1e0 && startcode <= 0x1ef) ||
298          (startcode == 0x1bd) || (startcode == 0x1fd)))
299        goto redo;
300    if (ppos) {
301        *ppos = avio_tell(s->pb) - 4;
302    }
303    len = avio_rb16(s->pb);
304    pts =
305    dts = AV_NOPTS_VALUE;
306    /* stuffing */
307    for(;;) {
308        if (len < 1)
309            goto error_redo;
310        c = avio_r8(s->pb);
311        len--;
312        /* XXX: for mpeg1, should test only bit 7 */
313        if (c != 0xff)
314            break;
315    }
316    if ((c & 0xc0) == 0x40) {
317        /* buffer scale & size */
318        avio_r8(s->pb);
319        c = avio_r8(s->pb);
320        len -= 2;
321    }
322    if ((c & 0xe0) == 0x20) {
323        dts = pts = get_pts(s->pb, c);
324        len -= 4;
325        if (c & 0x10){
326            dts = get_pts(s->pb, -1);
327            len -= 5;
328        }
329    } else if ((c & 0xc0) == 0x80) {
330        /* mpeg 2 PES */
331#if 0 /* some streams have this field set for no apparent reason */
332        if ((c & 0x30) != 0) {
333            /* Encrypted multiplex not handled */
334            goto redo;
335        }
336#endif
337        flags = avio_r8(s->pb);
338        header_len = avio_r8(s->pb);
339        len -= 2;
340        if (header_len > len)
341            goto error_redo;
342        len -= header_len;
343        if (flags & 0x80) {
344            dts = pts = get_pts(s->pb, -1);
345            header_len -= 5;
346            if (flags & 0x40) {
347                dts = get_pts(s->pb, -1);
348                header_len -= 5;
349            }
350        }
351        if (flags & 0x3f && header_len == 0){
352            flags &= 0xC0;
353            av_log(s, AV_LOG_WARNING, "Further flags set but no bytes left\n");
354        }
355        if (flags & 0x01) { /* PES extension */
356            pes_ext = avio_r8(s->pb);
357            header_len--;
358            /* Skip PES private data, program packet sequence counter and P-STD buffer */
359            skip = (pes_ext >> 4) & 0xb;
360            skip += skip & 0x9;
361            if (pes_ext & 0x40 || skip > header_len){
362                av_log(s, AV_LOG_WARNING, "pes_ext %X is invalid\n", pes_ext);
363                pes_ext=skip=0;
364            }
365            avio_skip(s->pb, skip);
366            header_len -= skip;
367
368            if (pes_ext & 0x01) { /* PES extension 2 */
369                ext2_len = avio_r8(s->pb);
370                header_len--;
371                if ((ext2_len & 0x7f) > 0) {
372                    id_ext = avio_r8(s->pb);
373                    if ((id_ext & 0x80) == 0)
374                        startcode = ((startcode & 0xff) << 8) | id_ext;
375                    header_len--;
376                }
377            }
378        }
379        if(header_len < 0)
380            goto error_redo;
381        avio_skip(s->pb, header_len);
382    }
383    else if( c!= 0xf )
384        goto redo;
385
386    if (startcode == PRIVATE_STREAM_1 && !m->psm_es_type[startcode & 0xff]) {
387        startcode = avio_r8(s->pb);
388        len--;
389        if (startcode >= 0x80 && startcode <= 0xcf) {
390            /* audio: skip header */
391            avio_r8(s->pb);
392            avio_r8(s->pb);
393            avio_r8(s->pb);
394            len -= 3;
395            if (startcode >= 0xb0 && startcode <= 0xbf) {
396                /* MLP/TrueHD audio has a 4-byte header */
397                avio_r8(s->pb);
398                len--;
399            }
400        }
401    }
402    if(len<0)
403        goto error_redo;
404    if(dts != AV_NOPTS_VALUE && ppos){
405        int i;
406        for(i=0; i<s->nb_streams; i++){
407            if(startcode == s->streams[i]->id &&
408               s->pb->seekable /* index useless on streams anyway */) {
409                ff_reduce_index(s, i);
410                av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
411            }
412        }
413    }
414
415    *pstart_code = startcode;
416    *ppts = pts;
417    *pdts = dts;
418    return len;
419}
420
421static int mpegps_read_packet(AVFormatContext *s,
422                              AVPacket *pkt)
423{
424    MpegDemuxContext *m = s->priv_data;
425    AVStream *st;
426    int len, startcode, i, es_type, ret;
427    enum CodecID codec_id = CODEC_ID_NONE;
428    enum AVMediaType type;
429    int64_t pts, dts, dummy_pos; //dummy_pos is needed for the index building to work
430    uint8_t av_uninit(dvdaudio_substream_type);
431
432 redo:
433    len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts);
434    if (len < 0)
435        return len;
436
437    if(startcode == 0x1bd) {
438        dvdaudio_substream_type = avio_r8(s->pb);
439        avio_skip(s->pb, 3);
440        len -= 4;
441    }
442
443    /* now find stream */
444    for(i=0;i<s->nb_streams;i++) {
445        st = s->streams[i];
446        if (st->id == startcode)
447            goto found;
448    }
449
450    es_type = m->psm_es_type[startcode & 0xff];
451    if(es_type > 0 && es_type != STREAM_TYPE_PRIVATE_DATA){
452        if(es_type == STREAM_TYPE_VIDEO_MPEG1){
453            codec_id = CODEC_ID_MPEG2VIDEO;
454            type = AVMEDIA_TYPE_VIDEO;
455        } else if(es_type == STREAM_TYPE_VIDEO_MPEG2){
456            codec_id = CODEC_ID_MPEG2VIDEO;
457            type = AVMEDIA_TYPE_VIDEO;
458        } else if(es_type == STREAM_TYPE_AUDIO_MPEG1 ||
459                  es_type == STREAM_TYPE_AUDIO_MPEG2){
460            codec_id = CODEC_ID_MP3;
461            type = AVMEDIA_TYPE_AUDIO;
462        } else if(es_type == STREAM_TYPE_AUDIO_AAC){
463            codec_id = CODEC_ID_AAC;
464            type = AVMEDIA_TYPE_AUDIO;
465        } else if(es_type == STREAM_TYPE_VIDEO_MPEG4){
466            codec_id = CODEC_ID_MPEG4;
467            type = AVMEDIA_TYPE_VIDEO;
468        } else if(es_type == STREAM_TYPE_VIDEO_H264){
469            codec_id = CODEC_ID_H264;
470            type = AVMEDIA_TYPE_VIDEO;
471        } else if(es_type == STREAM_TYPE_AUDIO_AC3){
472            codec_id = CODEC_ID_AC3;
473            type = AVMEDIA_TYPE_AUDIO;
474        } else {
475            goto skip;
476        }
477    } else if (startcode >= 0x1e0 && startcode <= 0x1ef) {
478        static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
479        unsigned char buf[8];
480        avio_read(s->pb, buf, 8);
481        avio_seek(s->pb, -8, SEEK_CUR);
482        if(!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1))
483            codec_id = CODEC_ID_CAVS;
484        else
485            codec_id = CODEC_ID_PROBE;
486        type = AVMEDIA_TYPE_VIDEO;
487    } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
488        type = AVMEDIA_TYPE_AUDIO;
489        codec_id = m->sofdec > 0 ? CODEC_ID_ADPCM_ADX : CODEC_ID_MP2;
490    } else if (startcode >= 0x80 && startcode <= 0x87) {
491        type = AVMEDIA_TYPE_AUDIO;
492        codec_id = CODEC_ID_AC3;
493    } else if (  ( startcode >= 0x88 && startcode <= 0x8f)
494               ||( startcode >= 0x98 && startcode <= 0x9f)) {
495        /* 0x90 - 0x97 is reserved for SDDS in DVD specs */
496        type = AVMEDIA_TYPE_AUDIO;
497        codec_id = CODEC_ID_DTS;
498    } else if (startcode >= 0xa0 && startcode <= 0xaf) {
499        type = AVMEDIA_TYPE_AUDIO;
500        /* 16 bit form will be handled as CODEC_ID_PCM_S16BE */
501        codec_id = CODEC_ID_PCM_DVD;
502    } else if (startcode >= 0xb0 && startcode <= 0xbf) {
503        type = AVMEDIA_TYPE_AUDIO;
504        codec_id = CODEC_ID_TRUEHD;
505    } else if (startcode >= 0xc0 && startcode <= 0xcf) {
506        /* Used for both AC-3 and E-AC-3 in EVOB files */
507        type = AVMEDIA_TYPE_AUDIO;
508        codec_id = CODEC_ID_AC3;
509    } else if (startcode >= 0x20 && startcode <= 0x3f) {
510        type = AVMEDIA_TYPE_SUBTITLE;
511        codec_id = CODEC_ID_DVD_SUBTITLE;
512    } else if (startcode >= 0xfd55 && startcode <= 0xfd5f) {
513        type = AVMEDIA_TYPE_VIDEO;
514        codec_id = CODEC_ID_VC1;
515    } else if (startcode == 0x1bd) {
516        // check dvd audio substream type
517        type = AVMEDIA_TYPE_AUDIO;
518        switch(dvdaudio_substream_type & 0xe0) {
519        case 0xa0:  codec_id = CODEC_ID_PCM_DVD;
520                    break;
521        case 0x80:  if((dvdaudio_substream_type & 0xf8) == 0x88)
522                         codec_id = CODEC_ID_DTS;
523                    else codec_id = CODEC_ID_AC3;
524                    break;
525        default:    av_log(s, AV_LOG_ERROR, "Unknown 0x1bd sub-stream\n");
526                    goto skip;
527        }
528    } else {
529    skip:
530        /* skip packet */
531        avio_skip(s->pb, len);
532        goto redo;
533    }
534    /* no stream found: add a new stream */
535    st = avformat_new_stream(s, NULL);
536    if (!st)
537        goto skip;
538    st->id = startcode;
539    st->codec->codec_type = type;
540    st->codec->codec_id = codec_id;
541    if (codec_id != CODEC_ID_PCM_S16BE)
542        st->need_parsing = AVSTREAM_PARSE_FULL;
543 found:
544    if(st->discard >= AVDISCARD_ALL)
545        goto skip;
546    if ((startcode >= 0xa0 && startcode <= 0xaf) ||
547        (startcode == 0x1bd && ((dvdaudio_substream_type & 0xe0) == 0xa0))) {
548        int b1, freq;
549
550        /* for LPCM, we just skip the header and consider it is raw
551           audio data */
552        if (len <= 3)
553            goto skip;
554        avio_r8(s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
555        b1 = avio_r8(s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
556        avio_r8(s->pb); /* dynamic range control (0x80 = off) */
557        len -= 3;
558        freq = (b1 >> 4) & 3;
559        st->codec->sample_rate = lpcm_freq_tab[freq];
560        st->codec->channels = 1 + (b1 & 7);
561        st->codec->bits_per_coded_sample = 16 + ((b1 >> 6) & 3) * 4;
562        st->codec->bit_rate = st->codec->channels *
563                              st->codec->sample_rate *
564                              st->codec->bits_per_coded_sample;
565        if (st->codec->bits_per_coded_sample == 16)
566            st->codec->codec_id = CODEC_ID_PCM_S16BE;
567        else if (st->codec->bits_per_coded_sample == 28)
568            return AVERROR(EINVAL);
569    }
570    ret = av_get_packet(s->pb, pkt, len);
571    pkt->pts = pts;
572    pkt->dts = dts;
573    pkt->pos = dummy_pos;
574    pkt->stream_index = st->index;
575    av_dlog(s, "%d: pts=%0.3f dts=%0.3f size=%d\n",
576            pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0,
577            pkt->size);
578
579    return (ret < 0) ? ret : 0;
580}
581
582static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
583                               int64_t *ppos, int64_t pos_limit)
584{
585    int len, startcode;
586    int64_t pos, pts, dts;
587
588    pos = *ppos;
589    if (avio_seek(s->pb, pos, SEEK_SET) < 0)
590        return AV_NOPTS_VALUE;
591
592    for(;;) {
593        len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts);
594        if (len < 0) {
595            av_dlog(s, "none (ret=%d)\n", len);
596            return AV_NOPTS_VALUE;
597        }
598        if (startcode == s->streams[stream_index]->id &&
599            dts != AV_NOPTS_VALUE) {
600            break;
601        }
602        avio_skip(s->pb, len);
603    }
604    av_dlog(s, "pos=0x%"PRIx64" dts=0x%"PRIx64" %0.3f\n",
605            pos, dts, dts / 90000.0);
606    *ppos = pos;
607    return dts;
608}
609
610AVInputFormat ff_mpegps_demuxer = {
611    .name           = "mpeg",
612    .long_name      = NULL_IF_CONFIG_SMALL("MPEG-PS format"),
613    .priv_data_size = sizeof(MpegDemuxContext),
614    .read_probe     = mpegps_probe,
615    .read_header    = mpegps_read_header,
616    .read_packet    = mpegps_read_packet,
617    .read_timestamp = mpegps_read_dts,
618    .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
619};
620