Statistics
| Branch: | Revision:

ffmpeg / libavformat / mpeg.c @ 0a5790d1

History | View | Annotate | Download (16.2 KB)

1
/*
2
 * MPEG1/2 demuxer
3
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg 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
 * FFmpeg 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 FFmpeg; 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 "mpeg.h"
24

    
25
//#define DEBUG_SEEK
26

    
27
#undef NDEBUG
28
#include <assert.h>
29

    
30
/*********************************************/
31
/* demux code */
32

    
33
#define MAX_SYNC_SIZE 100000
34

    
35
static int cdxa_probe(AVProbeData *p)
36
{
37
    /* check file header */
38
    if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
39
        p->buf[2] == 'F' && p->buf[3] == 'F' &&
40
        p->buf[8] == 'C' && p->buf[9] == 'D' &&
41
        p->buf[10] == 'X' && p->buf[11] == 'A')
42
        return AVPROBE_SCORE_MAX;
43
    else
44
        return 0;
45
}
46

    
47
static int mpegps_probe(AVProbeData *p)
48
{
49
    uint32_t code= -1;
50
    int sys=0, pspack=0, priv1=0, vid=0, audio=0;
51
    int i;
52
    int score=0;
53

    
54
    score = cdxa_probe(p);
55
    if (score > 0) return score;
56

    
57
    /* Search for MPEG stream */
58
    for(i=0; i<p->buf_size; i++){
59
        code = (code<<8) + p->buf[i];
60
        if ((code & 0xffffff00) == 0x100) {
61
            if(code == SYSTEM_HEADER_START_CODE) sys++;
62
            else if(code == PRIVATE_STREAM_1)    priv1++;
63
            else if(code == PACK_START_CODE)     pspack++;
64
            else if((code & 0xf0) == VIDEO_ID)   vid++;
65
            else if((code & 0xe0) == AUDIO_ID)   audio++;
66
        }
67
    }
68

    
69
    if(vid || audio)            /* invalid VDR files nd short PES streams */
70
        score= AVPROBE_SCORE_MAX/4;
71

    
72
//av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d len:%d\n", sys, priv1, pspack,vid, audio, p->buf_size);
73
    if(sys && sys*9 <= pspack*10)
74
        return AVPROBE_SCORE_MAX/2+2; // +1 for .mpg
75
    if((priv1 || vid || audio) && (priv1+vid+audio)*9 <= pspack*10)
76
        return AVPROBE_SCORE_MAX/2+2; // +1 for .mpg
77
    if((!!vid ^ !!audio) && (audio+vid > 1) && !sys && !pspack && p->buf_size>2048) /* PES stream */
78
        return AVPROBE_SCORE_MAX/2+2;
79

    
80
    //02-Penguin.flac has sys:0 priv1:0 pspack:0 vid:0 audio:1
81
    return score;
82
}
83

    
84

    
85
typedef struct MpegDemuxContext {
86
    int32_t header_state;
87
    unsigned char psm_es_type[256];
88
} MpegDemuxContext;
89

    
90
static int mpegps_read_header(AVFormatContext *s,
91
                              AVFormatParameters *ap)
92
{
93
    MpegDemuxContext *m = s->priv_data;
94
    m->header_state = 0xff;
95
    s->ctx_flags |= AVFMTCTX_NOHEADER;
96

    
97
    /* no need to do more */
98
    return 0;
99
}
100

    
101
static int64_t get_pts(ByteIOContext *pb, int c)
102
{
103
    int64_t pts;
104
    int val;
105

    
106
    if (c < 0)
107
        c = get_byte(pb);
108
    pts = (int64_t)((c >> 1) & 0x07) << 30;
109
    val = get_be16(pb);
110
    pts |= (int64_t)(val >> 1) << 15;
111
    val = get_be16(pb);
112
    pts |= (int64_t)(val >> 1);
113
    return pts;
114
}
115

    
116
static int find_next_start_code(ByteIOContext *pb, int *size_ptr,
117
                                int32_t *header_state)
118
{
119
    unsigned int state, v;
120
    int val, n;
121

    
122
    state = *header_state;
123
    n = *size_ptr;
124
    while (n > 0) {
125
        if (url_feof(pb))
126
            break;
127
        v = get_byte(pb);
128
        n--;
129
        if (state == 0x000001) {
130
            state = ((state << 8) | v) & 0xffffff;
131
            val = state;
132
            goto found;
133
        }
134
        state = ((state << 8) | v) & 0xffffff;
135
    }
136
    val = -1;
137
 found:
138
    *header_state = state;
139
    *size_ptr = n;
140
    return val;
141
}
142

    
143
#if 0 /* unused, remove? */
144
/* XXX: optimize */
145
static int find_prev_start_code(ByteIOContext *pb, int *size_ptr)
146
{
147
    int64_t pos, pos_start;
148
    int max_size, start_code;
149

150
    max_size = *size_ptr;
151
    pos_start = url_ftell(pb);
152

153
    /* in order to go faster, we fill the buffer */
154
    pos = pos_start - 16386;
155
    if (pos < 0)
156
        pos = 0;
157
    url_fseek(pb, pos, SEEK_SET);
158
    get_byte(pb);
159

160
    pos = pos_start;
161
    for(;;) {
162
        pos--;
163
        if (pos < 0 || (pos_start - pos) >= max_size) {
164
            start_code = -1;
165
            goto the_end;
166
        }
167
        url_fseek(pb, pos, SEEK_SET);
168
        start_code = get_be32(pb);
169
        if ((start_code & 0xffffff00) == 0x100)
170
            break;
171
    }
172
 the_end:
173
    *size_ptr = pos_start - pos;
174
    return start_code;
175
}
176
#endif
177

    
178
/**
179
 * Extracts stream types from a program stream map
180
 * According to ISO/IEC 13818-1 ('MPEG-2 Systems') table 2-35
181
 *
182
 * @return number of bytes occupied by PSM in the bitstream
183
 */
184
static long mpegps_psm_parse(MpegDemuxContext *m, ByteIOContext *pb)
185
{
186
    int psm_length, ps_info_length, es_map_length;
187

    
188
    psm_length = get_be16(pb);
189
    get_byte(pb);
190
    get_byte(pb);
191
    ps_info_length = get_be16(pb);
192

    
193
    /* skip program_stream_info */
194
    url_fskip(pb, ps_info_length);
195
    es_map_length = get_be16(pb);
196

    
197
    /* at least one es available? */
198
    while (es_map_length >= 4){
199
        unsigned char type = get_byte(pb);
200
        unsigned char es_id = get_byte(pb);
201
        uint16_t es_info_length = get_be16(pb);
202
        /* remember mapping from stream id to stream type */
203
        m->psm_es_type[es_id] = type;
204
        /* skip program_stream_info */
205
        url_fskip(pb, es_info_length);
206
        es_map_length -= 4 + es_info_length;
207
    }
208
    get_be32(pb); /* crc32 */
209
    return 2 + psm_length;
210
}
211

    
212
/* read the next PES header. Return its position in ppos
213
   (if not NULL), and its start code, pts and dts.
214
 */
215
static int mpegps_read_pes_header(AVFormatContext *s,
216
                                  int64_t *ppos, int *pstart_code,
217
                                  int64_t *ppts, int64_t *pdts)
218
{
219
    MpegDemuxContext *m = s->priv_data;
220
    int len, size, startcode, c, flags, header_len;
221
    int pes_ext, ext2_len, id_ext, skip;
222
    int64_t pts, dts;
223
    int64_t last_sync= url_ftell(&s->pb);
224

    
225
 error_redo:
226
        url_fseek(&s->pb, last_sync, SEEK_SET);
227
 redo:
228
        /* next start code (should be immediately after) */
229
        m->header_state = 0xff;
230
        size = MAX_SYNC_SIZE;
231
        startcode = find_next_start_code(&s->pb, &size, &m->header_state);
232
        last_sync = url_ftell(&s->pb);
233
    //printf("startcode=%x pos=0x%"PRIx64"\n", startcode, url_ftell(&s->pb));
234
    if (startcode < 0)
235
        return AVERROR(EIO);
236
    if (startcode == PACK_START_CODE)
237
        goto redo;
238
    if (startcode == SYSTEM_HEADER_START_CODE)
239
        goto redo;
240
    if (startcode == PADDING_STREAM ||
241
        startcode == PRIVATE_STREAM_2) {
242
        /* skip them */
243
        len = get_be16(&s->pb);
244
        url_fskip(&s->pb, len);
245
        goto redo;
246
    }
247
    if (startcode == PROGRAM_STREAM_MAP) {
248
        mpegps_psm_parse(m, &s->pb);
249
        goto redo;
250
    }
251

    
252
    /* find matching stream */
253
    if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
254
          (startcode >= 0x1e0 && startcode <= 0x1ef) ||
255
          (startcode == 0x1bd) || (startcode == 0x1fd)))
256
        goto redo;
257
    if (ppos) {
258
        *ppos = url_ftell(&s->pb) - 4;
259
    }
260
    len = get_be16(&s->pb);
261
    pts =
262
    dts = AV_NOPTS_VALUE;
263
    /* stuffing */
264
    for(;;) {
265
        if (len < 1)
266
            goto error_redo;
267
        c = get_byte(&s->pb);
268
        len--;
269
        /* XXX: for mpeg1, should test only bit 7 */
270
        if (c != 0xff)
271
            break;
272
    }
273
    if ((c & 0xc0) == 0x40) {
274
        /* buffer scale & size */
275
        get_byte(&s->pb);
276
        c = get_byte(&s->pb);
277
        len -= 2;
278
    }
279
    if ((c & 0xe0) == 0x20) {
280
        dts = pts = get_pts(&s->pb, c);
281
        len -= 4;
282
        if (c & 0x10){
283
            dts = get_pts(&s->pb, -1);
284
            len -= 5;
285
        }
286
    } else if ((c & 0xc0) == 0x80) {
287
        /* mpeg 2 PES */
288
#if 0 /* some streams have this field set for no apparent reason */
289
        if ((c & 0x30) != 0) {
290
            /* Encrypted multiplex not handled */
291
            goto redo;
292
        }
293
#endif
294
        flags = get_byte(&s->pb);
295
        header_len = get_byte(&s->pb);
296
        len -= 2;
297
        if (header_len > len)
298
            goto error_redo;
299
        len -= header_len;
300
        if (flags & 0x80) {
301
            dts = pts = get_pts(&s->pb, -1);
302
            header_len -= 5;
303
            if (flags & 0x40) {
304
                dts = get_pts(&s->pb, -1);
305
                header_len -= 5;
306
            }
307
        }
308
        if (flags & 0x01) { /* PES extension */
309
            pes_ext = get_byte(&s->pb);
310
            header_len--;
311
            if (pes_ext & 0x40) { /* pack header - should be zero in PS */
312
                goto error_redo;
313
            }
314
            /* Skip PES private data, program packet sequence counter and P-STD buffer */
315
            skip = (pes_ext >> 4) & 0xb;
316
            skip += skip & 0x9;
317
            url_fskip(&s->pb, skip);
318
            header_len -= skip;
319

    
320
            if (pes_ext & 0x01) { /* PES extension 2 */
321
                ext2_len = get_byte(&s->pb);
322
                header_len--;
323
                if ((ext2_len & 0x7f) > 0) {
324
                    id_ext = get_byte(&s->pb);
325
                    if ((id_ext & 0x80) == 0)
326
                        startcode = ((startcode & 0xff) << 8) | id_ext;
327
                    header_len--;
328
                }
329
            }
330
        }
331
        if(header_len < 0)
332
            goto error_redo;
333
        url_fskip(&s->pb, header_len);
334
    }
335
    else if( c!= 0xf )
336
        goto redo;
337

    
338
    if (startcode == PRIVATE_STREAM_1 && !m->psm_es_type[startcode & 0xff]) {
339
        startcode = get_byte(&s->pb);
340
        len--;
341
        if (startcode >= 0x80 && startcode <= 0xcf) {
342
            /* audio: skip header */
343
            get_byte(&s->pb);
344
            get_byte(&s->pb);
345
            get_byte(&s->pb);
346
            len -= 3;
347
            if (startcode >= 0xb0 && startcode <= 0xbf) {
348
                /* MLP/TrueHD audio has a 4-byte header */
349
                get_byte(&s->pb);
350
                len--;
351
            }
352
        }
353
    }
354
    if(len<0)
355
        goto error_redo;
356
    if(dts != AV_NOPTS_VALUE && ppos){
357
        int i;
358
        for(i=0; i<s->nb_streams; i++){
359
            if(startcode == s->streams[i]->id) {
360
                av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
361
            }
362
        }
363
    }
364

    
365
    *pstart_code = startcode;
366
    *ppts = pts;
367
    *pdts = dts;
368
    return len;
369
}
370

    
371
static int mpegps_read_packet(AVFormatContext *s,
372
                              AVPacket *pkt)
373
{
374
    MpegDemuxContext *m = s->priv_data;
375
    AVStream *st;
376
    int len, startcode, i, type, codec_id = 0, es_type;
377
    int64_t pts, dts, dummy_pos; //dummy_pos is needed for the index building to work
378

    
379
 redo:
380
    len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts);
381
    if (len < 0)
382
        return len;
383

    
384
    /* now find stream */
385
    for(i=0;i<s->nb_streams;i++) {
386
        st = s->streams[i];
387
        if (st->id == startcode)
388
            goto found;
389
    }
390

    
391
    es_type = m->psm_es_type[startcode & 0xff];
392
    if(es_type > 0){
393
        if(es_type == STREAM_TYPE_VIDEO_MPEG1){
394
            codec_id = CODEC_ID_MPEG2VIDEO;
395
            type = CODEC_TYPE_VIDEO;
396
        } else if(es_type == STREAM_TYPE_VIDEO_MPEG2){
397
            codec_id = CODEC_ID_MPEG2VIDEO;
398
            type = CODEC_TYPE_VIDEO;
399
        } else if(es_type == STREAM_TYPE_AUDIO_MPEG1 ||
400
                  es_type == STREAM_TYPE_AUDIO_MPEG2){
401
            codec_id = CODEC_ID_MP3;
402
            type = CODEC_TYPE_AUDIO;
403
        } else if(es_type == STREAM_TYPE_AUDIO_AAC){
404
            codec_id = CODEC_ID_AAC;
405
            type = CODEC_TYPE_AUDIO;
406
        } else if(es_type == STREAM_TYPE_VIDEO_MPEG4){
407
            codec_id = CODEC_ID_MPEG4;
408
            type = CODEC_TYPE_VIDEO;
409
        } else if(es_type == STREAM_TYPE_VIDEO_H264){
410
            codec_id = CODEC_ID_H264;
411
            type = CODEC_TYPE_VIDEO;
412
        } else if(es_type == STREAM_TYPE_AUDIO_AC3){
413
            codec_id = CODEC_ID_AC3;
414
            type = CODEC_TYPE_AUDIO;
415
        } else {
416
            goto skip;
417
        }
418
    } else if (startcode >= 0x1e0 && startcode <= 0x1ef) {
419
        static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
420
        unsigned char buf[8];
421
        get_buffer(&s->pb, buf, 8);
422
        url_fseek(&s->pb, -8, SEEK_CUR);
423
        if(!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1))
424
            codec_id = CODEC_ID_CAVS;
425
        else
426
            codec_id = CODEC_ID_MPEG2VIDEO;
427
        type = CODEC_TYPE_VIDEO;
428
    } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
429
        type = CODEC_TYPE_AUDIO;
430
        codec_id = CODEC_ID_MP2;
431
    } else if (startcode >= 0x80 && startcode <= 0x87) {
432
        type = CODEC_TYPE_AUDIO;
433
        codec_id = CODEC_ID_AC3;
434
    } else if ((startcode >= 0x88 && startcode <= 0x8f)
435
               ||( startcode >= 0x98 && startcode <= 0x9f)) {
436
        /* 0x90 - 0x97 is reserved for SDDS in DVD specs */
437
        type = CODEC_TYPE_AUDIO;
438
        codec_id = CODEC_ID_DTS;
439
    } else if (startcode >= 0xa0 && startcode <= 0xaf) {
440
        type = CODEC_TYPE_AUDIO;
441
        codec_id = CODEC_ID_PCM_S16BE;
442
    } else if (startcode >= 0xb0 && startcode <= 0xbf) {
443
        type = CODEC_TYPE_AUDIO;
444
        codec_id = CODEC_ID_MLP;
445
    } else if (startcode >= 0xc0 && startcode <= 0xcf) {
446
        /* Used for both AC-3 and E-AC-3 in EVOB files */
447
        type = CODEC_TYPE_AUDIO;
448
        codec_id = CODEC_ID_AC3;
449
    } else if (startcode >= 0x20 && startcode <= 0x3f) {
450
        type = CODEC_TYPE_SUBTITLE;
451
        codec_id = CODEC_ID_DVD_SUBTITLE;
452
    } else if (startcode >= 0xfd55 && startcode <= 0xfd5f) {
453
        type = CODEC_TYPE_VIDEO;
454
        codec_id = CODEC_ID_VC1;
455
    } else {
456
    skip:
457
        /* skip packet */
458
        url_fskip(&s->pb, len);
459
        goto redo;
460
    }
461
    /* no stream found: add a new stream */
462
    st = av_new_stream(s, startcode);
463
    if (!st)
464
        goto skip;
465
    st->codec->codec_type = type;
466
    st->codec->codec_id = codec_id;
467
    if (codec_id != CODEC_ID_PCM_S16BE)
468
        st->need_parsing = AVSTREAM_PARSE_FULL;
469
 found:
470
    if(st->discard >= AVDISCARD_ALL)
471
        goto skip;
472
    if (startcode >= 0xa0 && startcode <= 0xaf) {
473
        int b1, freq;
474

    
475
        /* for LPCM, we just skip the header and consider it is raw
476
           audio data */
477
        if (len <= 3)
478
            goto skip;
479
        get_byte(&s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
480
        b1 = get_byte(&s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
481
        get_byte(&s->pb); /* dynamic range control (0x80 = off) */
482
        len -= 3;
483
        freq = (b1 >> 4) & 3;
484
        st->codec->sample_rate = lpcm_freq_tab[freq];
485
        st->codec->channels = 1 + (b1 & 7);
486
        st->codec->bit_rate = st->codec->channels * st->codec->sample_rate * 2;
487
    }
488
    av_new_packet(pkt, len);
489
    get_buffer(&s->pb, pkt->data, pkt->size);
490
    pkt->pts = pts;
491
    pkt->dts = dts;
492
    pkt->stream_index = st->index;
493
#if 0
494
    av_log(s, AV_LOG_DEBUG, "%d: pts=%0.3f dts=%0.3f size=%d\n",
495
           pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0, pkt->size);
496
#endif
497

    
498
    return 0;
499
}
500

    
501
static int mpegps_read_close(AVFormatContext *s)
502
{
503
    return 0;
504
}
505

    
506
static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
507
                               int64_t *ppos, int64_t pos_limit)
508
{
509
    int len, startcode;
510
    int64_t pos, pts, dts;
511

    
512
    pos = *ppos;
513
#ifdef DEBUG_SEEK
514
    printf("read_dts: pos=0x%"PRIx64" next=%d -> ", pos, find_next);
515
#endif
516
    url_fseek(&s->pb, pos, SEEK_SET);
517
    for(;;) {
518
        len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts);
519
        if (len < 0) {
520
#ifdef DEBUG_SEEK
521
            printf("none (ret=%d)\n", len);
522
#endif
523
            return AV_NOPTS_VALUE;
524
        }
525
        if (startcode == s->streams[stream_index]->id &&
526
            dts != AV_NOPTS_VALUE) {
527
            break;
528
        }
529
        url_fskip(&s->pb, len);
530
    }
531
#ifdef DEBUG_SEEK
532
    printf("pos=0x%"PRIx64" dts=0x%"PRIx64" %0.3f\n", pos, dts, dts / 90000.0);
533
#endif
534
    *ppos = pos;
535
    return dts;
536
}
537

    
538
AVInputFormat mpegps_demuxer = {
539
    "mpeg",
540
    "MPEG PS format",
541
    sizeof(MpegDemuxContext),
542
    mpegps_probe,
543
    mpegps_read_header,
544
    mpegps_read_packet,
545
    mpegps_read_close,
546
    NULL, //mpegps_read_seek,
547
    mpegps_read_dts,
548
    .flags = AVFMT_SHOW_IDS,
549
};