Statistics
| Branch: | Revision:

ffmpeg / libavformat / mpeg.c @ 9d4edf96

History | View | Annotate | Download (16.2 KB)

1 de6d9b64 Fabrice Bellard
/*
2 2abe5a01 Baptiste Coudurier
 * MPEG1/2 demuxer
3 19720f15 Fabrice Bellard
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
4 de6d9b64 Fabrice Bellard
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 19720f15 Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 de6d9b64 Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 19720f15 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 de6d9b64 Fabrice Bellard
 */
21 2abe5a01 Baptiste Coudurier
22 de6d9b64 Fabrice Bellard
#include "avformat.h"
23 2abe5a01 Baptiste Coudurier
#include "mpeg.h"
24 de6d9b64 Fabrice Bellard
25 27f388aa Fabrice Bellard
//#define DEBUG_SEEK
26 de6d9b64 Fabrice Bellard
27 b754978a Michael Niedermayer
#undef NDEBUG
28
#include <assert.h>
29
30 de6d9b64 Fabrice Bellard
/*********************************************/
31
/* demux code */
32
33
#define MAX_SYNC_SIZE 100000
34
35 c5e33115 Allan Sandfeld Jensen
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 db7f1f95 Fabrice Bellard
static int mpegps_probe(AVProbeData *p)
48
{
49 9870a7bd Michael Niedermayer
    uint32_t code= -1;
50 19e0e403 Måns Rullgård
    int sys=0, pspack=0, priv1=0, vid=0, audio=0;
51 95f97de1 Michael Niedermayer
    int i;
52 a1c69e0b Michael Niedermayer
    int score=0;
53 db7f1f95 Fabrice Bellard
54 c5e33115 Allan Sandfeld Jensen
    score = cdxa_probe(p);
55
    if (score > 0) return score;
56
57
    /* Search for MPEG stream */
58 9870a7bd Michael Niedermayer
    for(i=0; i<p->buf_size; i++){
59
        code = (code<<8) + p->buf[i];
60 ec23a472 Isaac Richards
        if ((code & 0xffffff00) == 0x100) {
61 25c533a2 Måns Rullgård
            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 ec23a472 Isaac Richards
        }
67 db7f1f95 Fabrice Bellard
    }
68 a1c69e0b Michael Niedermayer
69
    if(vid || audio)            /* invalid VDR files nd short PES streams */
70
        score= AVPROBE_SCORE_MAX/4;
71
72 3504467f Michael Niedermayer
//av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d len:%d\n", sys, priv1, pspack,vid, audio, p->buf_size);
73 9870a7bd Michael Niedermayer
    if(sys && sys*9 <= pspack*10)
74
        return AVPROBE_SCORE_MAX/2+2; // +1 for .mpg
75 25c533a2 Måns Rullgård
    if((priv1 || vid || audio) && (priv1+vid+audio)*9 <= pspack*10)
76 0b2bb354 Michael Niedermayer
        return AVPROBE_SCORE_MAX/2+2; // +1 for .mpg
77 4cd25081 Michael Niedermayer
    if((!!vid ^ !!audio) && (audio+vid > 1) && !sys && !pspack && p->buf_size>2048) /* PES stream */
78 25c533a2 Måns Rullgård
        return AVPROBE_SCORE_MAX/2+2;
79 a1c69e0b Michael Niedermayer
80
    //02-Penguin.flac has sys:0 priv1:0 pspack:0 vid:0 audio:1
81
    return score;
82 db7f1f95 Fabrice Bellard
}
83
84
85 de6d9b64 Fabrice Bellard
typedef struct MpegDemuxContext {
86 191e8ca7 Måns Rullgård
    int32_t header_state;
87 e3d1cd8e Måns Rullgård
    unsigned char psm_es_type[256];
88 de6d9b64 Fabrice Bellard
} MpegDemuxContext;
89
90 27f388aa Fabrice Bellard
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 115329f1 Diego Biurrun
static int find_next_start_code(ByteIOContext *pb, int *size_ptr,
117 191e8ca7 Måns Rullgård
                                int32_t *header_state)
118 de6d9b64 Fabrice Bellard
{
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 88730be6 Måns Rullgård
#if 0 /* unused, remove? */
144 27f388aa Fabrice Bellard
/* XXX: optimize */
145
static int find_prev_start_code(ByteIOContext *pb, int *size_ptr)
146 de6d9b64 Fabrice Bellard
{
147 27f388aa Fabrice Bellard
    int64_t pos, pos_start;
148
    int max_size, start_code;
149 da24c5e3 Fabrice Bellard

150 27f388aa Fabrice Bellard
    max_size = *size_ptr;
151
    pos_start = url_ftell(pb);
152 de6d9b64 Fabrice Bellard

153 27f388aa Fabrice Bellard
    /* 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 de6d9b64 Fabrice Bellard

160 27f388aa Fabrice Bellard
    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 de6d9b64 Fabrice Bellard
}
176 88730be6 Måns Rullgård
#endif
177 de6d9b64 Fabrice Bellard
178 e3d1cd8e Måns Rullgård
/**
179
 * Extracts stream types from a program stream map
180
 * According to ISO/IEC 13818-1 ('MPEG-2 Systems') table 2-35
181 115329f1 Diego Biurrun
 *
182 e3d1cd8e Måns Rullgård
 * @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 115329f1 Diego Biurrun
/* read the next PES header. Return its position in ppos
213 27f388aa Fabrice Bellard
   (if not NULL), and its start code, pts and dts.
214
 */
215
static int mpegps_read_pes_header(AVFormatContext *s,
216 115329f1 Diego Biurrun
                                  int64_t *ppos, int *pstart_code,
217 8d14a25c Michael Niedermayer
                                  int64_t *ppts, int64_t *pdts)
218 de6d9b64 Fabrice Bellard
{
219
    MpegDemuxContext *m = s->priv_data;
220 27f388aa Fabrice Bellard
    int len, size, startcode, c, flags, header_len;
221 aad512b4 Michael Niedermayer
    int pes_ext, ext2_len, id_ext, skip;
222 e56cfad0 Michael Niedermayer
    int64_t pts, dts;
223
    int64_t last_sync= url_ftell(&s->pb);
224 de6d9b64 Fabrice Bellard
225 e56cfad0 Michael Niedermayer
 error_redo:
226
        url_fseek(&s->pb, last_sync, SEEK_SET);
227 de6d9b64 Fabrice Bellard
 redo:
228 27f388aa Fabrice Bellard
        /* 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 e56cfad0 Michael Niedermayer
        last_sync = url_ftell(&s->pb);
233 949b1a13 Steve L'Homme
    //printf("startcode=%x pos=0x%"PRIx64"\n", startcode, url_ftell(&s->pb));
234 de6d9b64 Fabrice Bellard
    if (startcode < 0)
235 6f3e0b21 Panagiotis Issaris
        return AVERROR(EIO);
236 de6d9b64 Fabrice Bellard
    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 e3d1cd8e Måns Rullgård
    if (startcode == PROGRAM_STREAM_MAP) {
248
        mpegps_psm_parse(m, &s->pb);
249
        goto redo;
250
    }
251 115329f1 Diego Biurrun
252 de6d9b64 Fabrice Bellard
    /* find matching stream */
253
    if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
254
          (startcode >= 0x1e0 && startcode <= 0x1ef) ||
255 aad512b4 Michael Niedermayer
          (startcode == 0x1bd) || (startcode == 0x1fd)))
256 de6d9b64 Fabrice Bellard
        goto redo;
257 27f388aa Fabrice Bellard
    if (ppos) {
258
        *ppos = url_ftell(&s->pb) - 4;
259
    }
260 de6d9b64 Fabrice Bellard
    len = get_be16(&s->pb);
261 75a9fbb9 Michael Niedermayer
    pts =
262 b2cac184 Fabrice Bellard
    dts = AV_NOPTS_VALUE;
263 de6d9b64 Fabrice Bellard
    /* stuffing */
264
    for(;;) {
265 27f388aa Fabrice Bellard
        if (len < 1)
266 e56cfad0 Michael Niedermayer
            goto error_redo;
267 de6d9b64 Fabrice Bellard
        c = get_byte(&s->pb);
268
        len--;
269
        /* XXX: for mpeg1, should test only bit 7 */
270 115329f1 Diego Biurrun
        if (c != 0xff)
271 de6d9b64 Fabrice Bellard
            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 b90ba24b Michael Niedermayer
    if ((c & 0xe0) == 0x20) {
280 27f388aa Fabrice Bellard
        dts = pts = get_pts(&s->pb, c);
281 de6d9b64 Fabrice Bellard
        len -= 4;
282 b90ba24b Michael Niedermayer
        if (c & 0x10){
283
            dts = get_pts(&s->pb, -1);
284
            len -= 5;
285
        }
286 de6d9b64 Fabrice Bellard
    } else if ((c & 0xc0) == 0x80) {
287
        /* mpeg 2 PES */
288 d8bee8dd Måns Rullgård
#if 0 /* some streams have this field set for no apparent reason */
289 de6d9b64 Fabrice Bellard
        if ((c & 0x30) != 0) {
290 27f388aa Fabrice Bellard
            /* Encrypted multiplex not handled */
291
            goto redo;
292 de6d9b64 Fabrice Bellard
        }
293 d8bee8dd Måns Rullgård
#endif
294 de6d9b64 Fabrice Bellard
        flags = get_byte(&s->pb);
295
        header_len = get_byte(&s->pb);
296
        len -= 2;
297
        if (header_len > len)
298 e56cfad0 Michael Niedermayer
            goto error_redo;
299 80036204 Michael Niedermayer
        len -= header_len;
300 b90ba24b Michael Niedermayer
        if (flags & 0x80) {
301 27f388aa Fabrice Bellard
            dts = pts = get_pts(&s->pb, -1);
302 de6d9b64 Fabrice Bellard
            header_len -= 5;
303 b90ba24b Michael Niedermayer
            if (flags & 0x40) {
304
                dts = get_pts(&s->pb, -1);
305
                header_len -= 5;
306
            }
307 de6d9b64 Fabrice Bellard
        }
308 aad512b4 Michael Niedermayer
        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 80036204 Michael Niedermayer
        if(header_len < 0)
332
            goto error_redo;
333
        url_fskip(&s->pb, header_len);
334 de6d9b64 Fabrice Bellard
    }
335 df70de1f Dmitry Borisov
    else if( c!= 0xf )
336
        goto redo;
337
338 e3d1cd8e Måns Rullgård
    if (startcode == PRIVATE_STREAM_1 && !m->psm_es_type[startcode & 0xff]) {
339 de6d9b64 Fabrice Bellard
        startcode = get_byte(&s->pb);
340
        len--;
341 aad512b4 Michael Niedermayer
        if (startcode >= 0x80 && startcode <= 0xcf) {
342 de6d9b64 Fabrice Bellard
            /* audio: skip header */
343
            get_byte(&s->pb);
344
            get_byte(&s->pb);
345
            get_byte(&s->pb);
346
            len -= 3;
347 aad512b4 Michael Niedermayer
            if (startcode >= 0xb0 && startcode <= 0xbf) {
348
                /* MLP/TrueHD audio has a 4-byte header */
349
                get_byte(&s->pb);
350
                len--;
351
            }
352 de6d9b64 Fabrice Bellard
        }
353
    }
354 7e4709b1 Michael Niedermayer
    if(len<0)
355
        goto error_redo;
356 b754978a Michael Niedermayer
    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 30a43f2d Michael Niedermayer
                av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
361 b754978a Michael Niedermayer
            }
362
        }
363
    }
364 115329f1 Diego Biurrun
365 27f388aa Fabrice Bellard
    *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 e3d1cd8e Måns Rullgård
    MpegDemuxContext *m = s->priv_data;
375 27f388aa Fabrice Bellard
    AVStream *st;
376 e3d1cd8e Måns Rullgård
    int len, startcode, i, type, codec_id = 0, es_type;
377 b754978a Michael Niedermayer
    int64_t pts, dts, dummy_pos; //dummy_pos is needed for the index building to work
378 27f388aa Fabrice Bellard
379
 redo:
380 8d14a25c Michael Niedermayer
    len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts);
381 27f388aa Fabrice Bellard
    if (len < 0)
382
        return len;
383 115329f1 Diego Biurrun
384 de6d9b64 Fabrice Bellard
    /* 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 e3d1cd8e Måns Rullgård
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 83d07313 Måns Rullgård
        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 db7f1f95 Fabrice Bellard
        type = CODEC_TYPE_VIDEO;
428
    } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
429
        type = CODEC_TYPE_AUDIO;
430
        codec_id = CODEC_ID_MP2;
431 3f2bf07b Joakim Plate
    } else if (startcode >= 0x80 && startcode <= 0x87) {
432 db7f1f95 Fabrice Bellard
        type = CODEC_TYPE_AUDIO;
433
        codec_id = CODEC_ID_AC3;
434 aad512b4 Michael Niedermayer
    } else if ((startcode >= 0x88 && startcode <= 0x8f)
435
               ||( startcode >= 0x98 && startcode <= 0x9f)) {
436
        /* 0x90 - 0x97 is reserved for SDDS in DVD specs */
437 23c99253 Michael Niedermayer
        type = CODEC_TYPE_AUDIO;
438
        codec_id = CODEC_ID_DTS;
439 aad512b4 Michael Niedermayer
    } else if (startcode >= 0xa0 && startcode <= 0xaf) {
440 9ec05e36 Fabrice Bellard
        type = CODEC_TYPE_AUDIO;
441
        codec_id = CODEC_ID_PCM_S16BE;
442 aad512b4 Michael Niedermayer
    } 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 a9c3213f Fabrice Bellard
    } else if (startcode >= 0x20 && startcode <= 0x3f) {
450
        type = CODEC_TYPE_SUBTITLE;
451
        codec_id = CODEC_ID_DVD_SUBTITLE;
452 aad512b4 Michael Niedermayer
    } else if (startcode >= 0xfd55 && startcode <= 0xfd5f) {
453
        type = CODEC_TYPE_VIDEO;
454
        codec_id = CODEC_ID_VC1;
455 db7f1f95 Fabrice Bellard
    } else {
456
    skip:
457
        /* skip packet */
458
        url_fskip(&s->pb, len);
459
        goto redo;
460
    }
461 1e5c667c Fabrice Bellard
    /* no stream found: add a new stream */
462
    st = av_new_stream(s, startcode);
463 115329f1 Diego Biurrun
    if (!st)
464 1e5c667c Fabrice Bellard
        goto skip;
465 01f4895c Michael Niedermayer
    st->codec->codec_type = type;
466
    st->codec->codec_id = codec_id;
467 27f388aa Fabrice Bellard
    if (codec_id != CODEC_ID_PCM_S16BE)
468 57004ff1 Aurelien Jacobs
        st->need_parsing = AVSTREAM_PARSE_FULL;
469 de6d9b64 Fabrice Bellard
 found:
470 f3356e9c Michael Niedermayer
    if(st->discard >= AVDISCARD_ALL)
471 b9866ebc Michael Niedermayer
        goto skip;
472 aad512b4 Michael Niedermayer
    if (startcode >= 0xa0 && startcode <= 0xaf) {
473 9ec05e36 Fabrice Bellard
        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 01f4895c Michael Niedermayer
        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 9ec05e36 Fabrice Bellard
    }
488 de6d9b64 Fabrice Bellard
    av_new_packet(pkt, len);
489
    get_buffer(&s->pb, pkt->data, pkt->size);
490
    pkt->pts = pts;
491 27f388aa Fabrice Bellard
    pkt->dts = dts;
492 db7f1f95 Fabrice Bellard
    pkt->stream_index = st->index;
493 27a206e0 Michel Bardiaux
#if 0
494 b9866ebc Michael Niedermayer
    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 27a206e0 Michel Bardiaux
#endif
497 0bd586c5 Mike Melanson
498 de6d9b64 Fabrice Bellard
    return 0;
499
}
500
501 db7f1f95 Fabrice Bellard
static int mpegps_read_close(AVFormatContext *s)
502 de6d9b64 Fabrice Bellard
{
503
    return 0;
504
}
505
506 115329f1 Diego Biurrun
static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
507 8d14a25c Michael Niedermayer
                               int64_t *ppos, int64_t pos_limit)
508 27f388aa Fabrice Bellard
{
509
    int len, startcode;
510
    int64_t pos, pts, dts;
511
512
    pos = *ppos;
513
#ifdef DEBUG_SEEK
514 949b1a13 Steve L'Homme
    printf("read_dts: pos=0x%"PRIx64" next=%d -> ", pos, find_next);
515 27f388aa Fabrice Bellard
#endif
516
    url_fseek(&s->pb, pos, SEEK_SET);
517
    for(;;) {
518 8d14a25c Michael Niedermayer
        len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts);
519 27f388aa Fabrice Bellard
        if (len < 0) {
520
#ifdef DEBUG_SEEK
521
            printf("none (ret=%d)\n", len);
522
#endif
523
            return AV_NOPTS_VALUE;
524
        }
525 115329f1 Diego Biurrun
        if (startcode == s->streams[stream_index]->id &&
526 27f388aa Fabrice Bellard
            dts != AV_NOPTS_VALUE) {
527
            break;
528
        }
529 8d14a25c Michael Niedermayer
        url_fskip(&s->pb, len);
530 27f388aa Fabrice Bellard
    }
531
#ifdef DEBUG_SEEK
532 949b1a13 Steve L'Homme
    printf("pos=0x%"PRIx64" dts=0x%"PRIx64" %0.3f\n", pos, dts, dts / 90000.0);
533 27f388aa Fabrice Bellard
#endif
534
    *ppos = pos;
535 cdd5034f Michael Niedermayer
    return dts;
536 27f388aa Fabrice Bellard
}
537
538 d2a067d1 Måns Rullgård
AVInputFormat mpegps_demuxer = {
539 db7f1f95 Fabrice Bellard
    "mpeg",
540
    "MPEG PS format",
541
    sizeof(MpegDemuxContext),
542
    mpegps_probe,
543
    mpegps_read_header,
544
    mpegps_read_packet,
545
    mpegps_read_close,
546 8d14a25c Michael Niedermayer
    NULL, //mpegps_read_seek,
547
    mpegps_read_dts,
548 a9c3213f Fabrice Bellard
    .flags = AVFMT_SHOW_IDS,
549 de6d9b64 Fabrice Bellard
};