Statistics
| Branch: | Revision:

ffmpeg / libavformat / mpeg.c @ e356fc57

History | View | Annotate | Download (19.4 KB)

1 de6d9b64 Fabrice Bellard
/*
2 2abe5a01 Baptiste Coudurier
 * MPEG1/2 demuxer
3 406792e7 Diego Biurrun
 * 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 19711af5 Anton Khirnov
#include "internal.h"
24 2abe5a01 Baptiste Coudurier
#include "mpeg.h"
25 de6d9b64 Fabrice Bellard
26 27f388aa Fabrice Bellard
//#define DEBUG_SEEK
27 de6d9b64 Fabrice Bellard
28 b754978a Michael Niedermayer
#undef NDEBUG
29
#include <assert.h>
30
31 de6d9b64 Fabrice Bellard
/*********************************************/
32
/* demux code */
33
34
#define MAX_SYNC_SIZE 100000
35
36 c6dcd0d7 Michael Niedermayer
static int check_pes(uint8_t *p, uint8_t *end){
37
    int pes1;
38
    int pes2=      (p[3] & 0xC0) == 0x80
39
                && (p[4] & 0xC0) != 0x40
40
                &&((p[4] & 0xC0) == 0x00 || (p[4]&0xC0)>>2 == (p[6]&0xF0));
41
42
    for(p+=3; p<end && *p == 0xFF; p++);
43
    if((*p&0xC0) == 0x40) p+=2;
44
    if((*p&0xF0) == 0x20){
45
        pes1= p[0]&p[2]&p[4]&1;
46
    }else if((*p&0xF0) == 0x30){
47
        pes1= p[0]&p[2]&p[4]&p[5]&p[7]&p[9]&1;
48
    }else
49
        pes1 = *p == 0x0F;
50
51
    return pes1||pes2;
52
}
53
54 db7f1f95 Fabrice Bellard
static int mpegps_probe(AVProbeData *p)
55
{
56 9870a7bd Michael Niedermayer
    uint32_t code= -1;
57 7e1720de Michael Niedermayer
    int sys=0, pspack=0, priv1=0, vid=0, audio=0, invalid=0;
58 95f97de1 Michael Niedermayer
    int i;
59 a1c69e0b Michael Niedermayer
    int score=0;
60 db7f1f95 Fabrice Bellard
61 9870a7bd Michael Niedermayer
    for(i=0; i<p->buf_size; i++){
62
        code = (code<<8) + p->buf[i];
63 ec23a472 Isaac Richards
        if ((code & 0xffffff00) == 0x100) {
64 612dc023 Janne Grunau
            int len= p->buf[i+1] << 8 | p->buf[i+2];
65 0c904db1 Michael Niedermayer
            int pes= check_pes(p->buf+i, p->buf+p->buf_size);
66 c6dcd0d7 Michael Niedermayer
67 25c533a2 Måns Rullgård
            if(code == SYSTEM_HEADER_START_CODE) sys++;
68
            else if(code == PACK_START_CODE)     pspack++;
69 274335e7 Michael Niedermayer
            else if((code & 0xf0) == VIDEO_ID &&  pes) vid++;
70 612dc023 Janne Grunau
            // skip pes payload to avoid start code emulation for private
71
            // and audio streams
72
            else if((code & 0xe0) == AUDIO_ID &&  pes) {audio++; i+=len;}
73
            else if(code == PRIVATE_STREAM_1  &&  pes) {priv1++; i+=len;}
74 7e1720de Michael Niedermayer
75
            else if((code & 0xf0) == VIDEO_ID && !pes) invalid++;
76
            else if((code & 0xe0) == AUDIO_ID && !pes) invalid++;
77 612dc023 Janne Grunau
            else if(code == PRIVATE_STREAM_1  && !pes) invalid++;
78 ec23a472 Isaac Richards
        }
79 db7f1f95 Fabrice Bellard
    }
80 a1c69e0b Michael Niedermayer
81 7e1720de Michael Niedermayer
    if(vid+audio > invalid)     /* invalid VDR files nd short PES streams */
82 a1c69e0b Michael Niedermayer
        score= AVPROBE_SCORE_MAX/4;
83
84 6de5b627 Michael Niedermayer
//av_log(NULL, AV_LOG_ERROR, "%d %d %d %d %d %d len:%d\n", sys, priv1, pspack,vid, audio, invalid, p->buf_size);
85 7e1720de Michael Niedermayer
    if(sys>invalid && sys*9 <= pspack*10)
86 c4674a47 Michael Niedermayer
        return pspack > 2 ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4; // +1 for .mpg
87 55f65247 Michael Niedermayer
    if(pspack > invalid && (priv1+vid+audio)*10 >= pspack*9)
88 c4674a47 Michael Niedermayer
        return pspack > 2 ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4; // +1 for .mpg
89
    if((!!vid ^ !!audio) && (audio > 4 || vid > 1) && !sys && !pspack && p->buf_size>2048 && vid + audio > invalid) /* PES stream */
90 3489e150 Reimar Döffinger
        return (audio > 12 || vid > 3) ? AVPROBE_SCORE_MAX/2+2 : AVPROBE_SCORE_MAX/4;
91 a1c69e0b Michael Niedermayer
92
    //02-Penguin.flac has sys:0 priv1:0 pspack:0 vid:0 audio:1
93 3489e150 Reimar Döffinger
    //mp3_misidentified_2.mp3 has sys:0 priv1:0 pspack:0 vid:0 audio:6
94 a1c69e0b Michael Niedermayer
    return score;
95 db7f1f95 Fabrice Bellard
}
96
97
98 de6d9b64 Fabrice Bellard
typedef struct MpegDemuxContext {
99 191e8ca7 Måns Rullgård
    int32_t header_state;
100 e3d1cd8e Måns Rullgård
    unsigned char psm_es_type[256];
101 8cd4ac3d Aurelien Jacobs
    int sofdec;
102 de6d9b64 Fabrice Bellard
} MpegDemuxContext;
103
104 27f388aa Fabrice Bellard
static int mpegps_read_header(AVFormatContext *s,
105
                              AVFormatParameters *ap)
106
{
107
    MpegDemuxContext *m = s->priv_data;
108 2c187847 Måns Rullgård
    const char *sofdec = "Sofdec";
109
    int v, i = 0;
110 8cd4ac3d Aurelien Jacobs
111 27f388aa Fabrice Bellard
    m->header_state = 0xff;
112
    s->ctx_flags |= AVFMTCTX_NOHEADER;
113
114 2c187847 Måns Rullgård
    m->sofdec = -1;
115
    do {
116 b7effd4e Anton Khirnov
        v = avio_r8(s->pb);
117 2c187847 Måns Rullgård
        m->header_state = m->header_state << 8 | v;
118
        m->sofdec++;
119
    } while (v == sofdec[i] && i++ < 6);
120 8cd4ac3d Aurelien Jacobs
121 711e69a7 tralph11@yahoo.com
    m->sofdec = (m->sofdec == 6) ? 1 : 0;
122
123 27f388aa Fabrice Bellard
    /* no need to do more */
124
    return 0;
125
}
126
127 ae628ec1 Anton Khirnov
static int64_t get_pts(AVIOContext *pb, int c)
128 27f388aa Fabrice Bellard
{
129 66e9e302 Ivo van Poorten
    uint8_t buf[5];
130
131 b7effd4e Anton Khirnov
    buf[0] = c<0 ? avio_r8(pb) : c;
132
    avio_read(pb, buf+1, 4);
133 66e9e302 Ivo van Poorten
134
    return ff_parse_pes_pts(buf);
135 27f388aa Fabrice Bellard
}
136
137 ae628ec1 Anton Khirnov
static int find_next_start_code(AVIOContext *pb, int *size_ptr,
138 191e8ca7 Måns Rullgård
                                int32_t *header_state)
139 de6d9b64 Fabrice Bellard
{
140
    unsigned int state, v;
141
    int val, n;
142
143
    state = *header_state;
144
    n = *size_ptr;
145
    while (n > 0) {
146
        if (url_feof(pb))
147
            break;
148 b7effd4e Anton Khirnov
        v = avio_r8(pb);
149 de6d9b64 Fabrice Bellard
        n--;
150
        if (state == 0x000001) {
151
            state = ((state << 8) | v) & 0xffffff;
152
            val = state;
153
            goto found;
154
        }
155
        state = ((state << 8) | v) & 0xffffff;
156
    }
157
    val = -1;
158
 found:
159
    *header_state = state;
160
    *size_ptr = n;
161
    return val;
162
}
163
164 88730be6 Måns Rullgård
#if 0 /* unused, remove? */
165 27f388aa Fabrice Bellard
/* XXX: optimize */
166 ae628ec1 Anton Khirnov
static int find_prev_start_code(AVIOContext *pb, int *size_ptr)
167 de6d9b64 Fabrice Bellard
{
168 27f388aa Fabrice Bellard
    int64_t pos, pos_start;
169
    int max_size, start_code;
170 da24c5e3 Fabrice Bellard

171 27f388aa Fabrice Bellard
    max_size = *size_ptr;
172
    pos_start = url_ftell(pb);
173 de6d9b64 Fabrice Bellard

174 27f388aa Fabrice Bellard
    /* in order to go faster, we fill the buffer */
175
    pos = pos_start - 16386;
176
    if (pos < 0)
177
        pos = 0;
178 6b4aa5da Anton Khirnov
    avio_seek(pb, pos, SEEK_SET);
179 b7effd4e Anton Khirnov
    avio_r8(pb);
180 de6d9b64 Fabrice Bellard

181 27f388aa Fabrice Bellard
    pos = pos_start;
182
    for(;;) {
183
        pos--;
184
        if (pos < 0 || (pos_start - pos) >= max_size) {
185
            start_code = -1;
186
            goto the_end;
187
        }
188 6b4aa5da Anton Khirnov
        avio_seek(pb, pos, SEEK_SET);
189 b7effd4e Anton Khirnov
        start_code = avio_rb32(pb);
190 27f388aa Fabrice Bellard
        if ((start_code & 0xffffff00) == 0x100)
191
            break;
192
    }
193
 the_end:
194
    *size_ptr = pos_start - pos;
195
    return start_code;
196 de6d9b64 Fabrice Bellard
}
197 88730be6 Måns Rullgård
#endif
198 de6d9b64 Fabrice Bellard
199 e3d1cd8e Måns Rullgård
/**
200 49bd8e4b Måns Rullgård
 * Extract stream types from a program stream map
201 e3d1cd8e Måns Rullgård
 * According to ISO/IEC 13818-1 ('MPEG-2 Systems') table 2-35
202 115329f1 Diego Biurrun
 *
203 e3d1cd8e Måns Rullgård
 * @return number of bytes occupied by PSM in the bitstream
204
 */
205 ae628ec1 Anton Khirnov
static long mpegps_psm_parse(MpegDemuxContext *m, AVIOContext *pb)
206 e3d1cd8e Måns Rullgård
{
207
    int psm_length, ps_info_length, es_map_length;
208
209 b7effd4e Anton Khirnov
    psm_length = avio_rb16(pb);
210
    avio_r8(pb);
211
    avio_r8(pb);
212
    ps_info_length = avio_rb16(pb);
213 e3d1cd8e Måns Rullgård
214
    /* skip program_stream_info */
215 e356fc57 Anton Khirnov
    avio_seek(pb, ps_info_length, SEEK_CUR);
216 b7effd4e Anton Khirnov
    es_map_length = avio_rb16(pb);
217 e3d1cd8e Måns Rullgård
218
    /* at least one es available? */
219
    while (es_map_length >= 4){
220 b7effd4e Anton Khirnov
        unsigned char type      = avio_r8(pb);
221
        unsigned char es_id     = avio_r8(pb);
222
        uint16_t es_info_length = avio_rb16(pb);
223 e3d1cd8e Måns Rullgård
        /* remember mapping from stream id to stream type */
224
        m->psm_es_type[es_id] = type;
225
        /* skip program_stream_info */
226 e356fc57 Anton Khirnov
        avio_seek(pb, es_info_length, SEEK_CUR);
227 e3d1cd8e Måns Rullgård
        es_map_length -= 4 + es_info_length;
228
    }
229 b7effd4e Anton Khirnov
    avio_rb32(pb); /* crc32 */
230 e3d1cd8e Måns Rullgård
    return 2 + psm_length;
231
}
232
233 115329f1 Diego Biurrun
/* read the next PES header. Return its position in ppos
234 27f388aa Fabrice Bellard
   (if not NULL), and its start code, pts and dts.
235
 */
236
static int mpegps_read_pes_header(AVFormatContext *s,
237 115329f1 Diego Biurrun
                                  int64_t *ppos, int *pstart_code,
238 8d14a25c Michael Niedermayer
                                  int64_t *ppts, int64_t *pdts)
239 de6d9b64 Fabrice Bellard
{
240
    MpegDemuxContext *m = s->priv_data;
241 27f388aa Fabrice Bellard
    int len, size, startcode, c, flags, header_len;
242 aad512b4 Michael Niedermayer
    int pes_ext, ext2_len, id_ext, skip;
243 e56cfad0 Michael Niedermayer
    int64_t pts, dts;
244 899681cd Björn Axelsson
    int64_t last_sync= url_ftell(s->pb);
245 de6d9b64 Fabrice Bellard
246 e56cfad0 Michael Niedermayer
 error_redo:
247 6b4aa5da Anton Khirnov
        avio_seek(s->pb, last_sync, SEEK_SET);
248 de6d9b64 Fabrice Bellard
 redo:
249 27f388aa Fabrice Bellard
        /* next start code (should be immediately after) */
250
        m->header_state = 0xff;
251
        size = MAX_SYNC_SIZE;
252 899681cd Björn Axelsson
        startcode = find_next_start_code(s->pb, &size, &m->header_state);
253
        last_sync = url_ftell(s->pb);
254
    //printf("startcode=%x pos=0x%"PRIx64"\n", startcode, url_ftell(s->pb));
255 0332324a Michael Niedermayer
    if (startcode < 0){
256
        if(url_feof(s->pb))
257
            return AVERROR_EOF;
258
        //FIXME we should remember header_state
259
        return AVERROR(EAGAIN);
260
    }
261
262 de6d9b64 Fabrice Bellard
    if (startcode == PACK_START_CODE)
263
        goto redo;
264
    if (startcode == SYSTEM_HEADER_START_CODE)
265
        goto redo;
266 2c187847 Måns Rullgård
    if (startcode == PADDING_STREAM) {
267 e356fc57 Anton Khirnov
        avio_seek(s->pb, avio_rb16(s->pb), SEEK_CUR);
268 2c187847 Måns Rullgård
        goto redo;
269
    }
270
    if (startcode == PRIVATE_STREAM_2) {
271 b7effd4e Anton Khirnov
        len = avio_rb16(s->pb);
272 2c187847 Måns Rullgård
        if (!m->sofdec) {
273
            while (len-- >= 6) {
274 b7effd4e Anton Khirnov
                if (avio_r8(s->pb) == 'S') {
275 2c187847 Måns Rullgård
                    uint8_t buf[5];
276 b7effd4e Anton Khirnov
                    avio_read(s->pb, buf, sizeof(buf));
277 2c187847 Måns Rullgård
                    m->sofdec = !memcmp(buf, "ofdec", 5);
278
                    len -= sizeof(buf);
279
                    break;
280
                }
281
            }
282
            m->sofdec -= !m->sofdec;
283
        }
284 e356fc57 Anton Khirnov
        avio_seek(s->pb, len, SEEK_CUR);
285 de6d9b64 Fabrice Bellard
        goto redo;
286
    }
287 e3d1cd8e Måns Rullgård
    if (startcode == PROGRAM_STREAM_MAP) {
288 899681cd Björn Axelsson
        mpegps_psm_parse(m, s->pb);
289 e3d1cd8e Måns Rullgård
        goto redo;
290
    }
291 115329f1 Diego Biurrun
292 de6d9b64 Fabrice Bellard
    /* find matching stream */
293
    if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
294
          (startcode >= 0x1e0 && startcode <= 0x1ef) ||
295 aad512b4 Michael Niedermayer
          (startcode == 0x1bd) || (startcode == 0x1fd)))
296 de6d9b64 Fabrice Bellard
        goto redo;
297 27f388aa Fabrice Bellard
    if (ppos) {
298 899681cd Björn Axelsson
        *ppos = url_ftell(s->pb) - 4;
299 27f388aa Fabrice Bellard
    }
300 b7effd4e Anton Khirnov
    len = avio_rb16(s->pb);
301 75a9fbb9 Michael Niedermayer
    pts =
302 b2cac184 Fabrice Bellard
    dts = AV_NOPTS_VALUE;
303 de6d9b64 Fabrice Bellard
    /* stuffing */
304
    for(;;) {
305 27f388aa Fabrice Bellard
        if (len < 1)
306 e56cfad0 Michael Niedermayer
            goto error_redo;
307 b7effd4e Anton Khirnov
        c = avio_r8(s->pb);
308 de6d9b64 Fabrice Bellard
        len--;
309
        /* XXX: for mpeg1, should test only bit 7 */
310 115329f1 Diego Biurrun
        if (c != 0xff)
311 de6d9b64 Fabrice Bellard
            break;
312
    }
313
    if ((c & 0xc0) == 0x40) {
314
        /* buffer scale & size */
315 b7effd4e Anton Khirnov
        avio_r8(s->pb);
316
        c = avio_r8(s->pb);
317 de6d9b64 Fabrice Bellard
        len -= 2;
318
    }
319 b90ba24b Michael Niedermayer
    if ((c & 0xe0) == 0x20) {
320 899681cd Björn Axelsson
        dts = pts = get_pts(s->pb, c);
321 de6d9b64 Fabrice Bellard
        len -= 4;
322 b90ba24b Michael Niedermayer
        if (c & 0x10){
323 899681cd Björn Axelsson
            dts = get_pts(s->pb, -1);
324 b90ba24b Michael Niedermayer
            len -= 5;
325
        }
326 de6d9b64 Fabrice Bellard
    } else if ((c & 0xc0) == 0x80) {
327
        /* mpeg 2 PES */
328 d8bee8dd Måns Rullgård
#if 0 /* some streams have this field set for no apparent reason */
329 de6d9b64 Fabrice Bellard
        if ((c & 0x30) != 0) {
330 27f388aa Fabrice Bellard
            /* Encrypted multiplex not handled */
331
            goto redo;
332 de6d9b64 Fabrice Bellard
        }
333 d8bee8dd Måns Rullgård
#endif
334 b7effd4e Anton Khirnov
        flags = avio_r8(s->pb);
335
        header_len = avio_r8(s->pb);
336 de6d9b64 Fabrice Bellard
        len -= 2;
337
        if (header_len > len)
338 e56cfad0 Michael Niedermayer
            goto error_redo;
339 80036204 Michael Niedermayer
        len -= header_len;
340 b90ba24b Michael Niedermayer
        if (flags & 0x80) {
341 899681cd Björn Axelsson
            dts = pts = get_pts(s->pb, -1);
342 de6d9b64 Fabrice Bellard
            header_len -= 5;
343 b90ba24b Michael Niedermayer
            if (flags & 0x40) {
344 899681cd Björn Axelsson
                dts = get_pts(s->pb, -1);
345 b90ba24b Michael Niedermayer
                header_len -= 5;
346
            }
347 de6d9b64 Fabrice Bellard
        }
348 675b8390 Michael Niedermayer
        if (flags & 0x3f && header_len == 0){
349
            flags &= 0xC0;
350
            av_log(s, AV_LOG_WARNING, "Further flags set but no bytes left\n");
351
        }
352 aad512b4 Michael Niedermayer
        if (flags & 0x01) { /* PES extension */
353 b7effd4e Anton Khirnov
            pes_ext = avio_r8(s->pb);
354 aad512b4 Michael Niedermayer
            header_len--;
355
            /* Skip PES private data, program packet sequence counter and P-STD buffer */
356
            skip = (pes_ext >> 4) & 0xb;
357
            skip += skip & 0x9;
358 b22d0c04 Michael Niedermayer
            if (pes_ext & 0x40 || skip > header_len){
359
                av_log(s, AV_LOG_WARNING, "pes_ext %X is invalid\n", pes_ext);
360
                pes_ext=skip=0;
361
            }
362 e356fc57 Anton Khirnov
            avio_seek(s->pb, skip, SEEK_CUR);
363 aad512b4 Michael Niedermayer
            header_len -= skip;
364
365
            if (pes_ext & 0x01) { /* PES extension 2 */
366 b7effd4e Anton Khirnov
                ext2_len = avio_r8(s->pb);
367 aad512b4 Michael Niedermayer
                header_len--;
368
                if ((ext2_len & 0x7f) > 0) {
369 b7effd4e Anton Khirnov
                    id_ext = avio_r8(s->pb);
370 aad512b4 Michael Niedermayer
                    if ((id_ext & 0x80) == 0)
371
                        startcode = ((startcode & 0xff) << 8) | id_ext;
372
                    header_len--;
373
                }
374
            }
375
        }
376 80036204 Michael Niedermayer
        if(header_len < 0)
377
            goto error_redo;
378 e356fc57 Anton Khirnov
        avio_seek(s->pb, header_len, SEEK_CUR);
379 de6d9b64 Fabrice Bellard
    }
380 df70de1f Dmitry Borisov
    else if( c!= 0xf )
381
        goto redo;
382
383 e3d1cd8e Måns Rullgård
    if (startcode == PRIVATE_STREAM_1 && !m->psm_es_type[startcode & 0xff]) {
384 b7effd4e Anton Khirnov
        startcode = avio_r8(s->pb);
385 de6d9b64 Fabrice Bellard
        len--;
386 aad512b4 Michael Niedermayer
        if (startcode >= 0x80 && startcode <= 0xcf) {
387 de6d9b64 Fabrice Bellard
            /* audio: skip header */
388 b7effd4e Anton Khirnov
            avio_r8(s->pb);
389
            avio_r8(s->pb);
390
            avio_r8(s->pb);
391 de6d9b64 Fabrice Bellard
            len -= 3;
392 aad512b4 Michael Niedermayer
            if (startcode >= 0xb0 && startcode <= 0xbf) {
393
                /* MLP/TrueHD audio has a 4-byte header */
394 b7effd4e Anton Khirnov
                avio_r8(s->pb);
395 aad512b4 Michael Niedermayer
                len--;
396
            }
397 de6d9b64 Fabrice Bellard
        }
398
    }
399 7e4709b1 Michael Niedermayer
    if(len<0)
400
        goto error_redo;
401 b754978a Michael Niedermayer
    if(dts != AV_NOPTS_VALUE && ppos){
402
        int i;
403
        for(i=0; i<s->nb_streams; i++){
404 6ba90c20 Michel Bardiaux
            if(startcode == s->streams[i]->id &&
405
               !url_is_streamed(s->pb) /* index useless on streams anyway */) {
406 3dea63bd Paul Kelly
                ff_reduce_index(s, i);
407 30a43f2d Michael Niedermayer
                av_add_index_entry(s->streams[i], *ppos, dts, 0, 0, AVINDEX_KEYFRAME /* FIXME keyframe? */);
408 b754978a Michael Niedermayer
            }
409
        }
410
    }
411 115329f1 Diego Biurrun
412 27f388aa Fabrice Bellard
    *pstart_code = startcode;
413
    *ppts = pts;
414
    *pdts = dts;
415
    return len;
416
}
417
418
static int mpegps_read_packet(AVFormatContext *s,
419
                              AVPacket *pkt)
420
{
421 e3d1cd8e Måns Rullgård
    MpegDemuxContext *m = s->priv_data;
422 27f388aa Fabrice Bellard
    AVStream *st;
423 fb65d2ca Diego Pettenò
    int len, startcode, i, es_type;
424
    enum CodecID codec_id = CODEC_ID_NONE;
425 72415b2a Stefano Sabatini
    enum AVMediaType type;
426 b754978a Michael Niedermayer
    int64_t pts, dts, dummy_pos; //dummy_pos is needed for the index building to work
427 1aeb55a1 Diego Biurrun
    uint8_t av_uninit(dvdaudio_substream_type);
428 27f388aa Fabrice Bellard
429
 redo:
430 8d14a25c Michael Niedermayer
    len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts);
431 27f388aa Fabrice Bellard
    if (len < 0)
432
        return len;
433 115329f1 Diego Biurrun
434 80e58c61 Benoit Fouet
    if(startcode == 0x1bd) {
435 b7effd4e Anton Khirnov
        dvdaudio_substream_type = avio_r8(s->pb);
436 e356fc57 Anton Khirnov
        avio_seek(s->pb, 3, SEEK_CUR);
437 80e58c61 Benoit Fouet
        len -= 4;
438
    }
439
440 de6d9b64 Fabrice Bellard
    /* now find stream */
441
    for(i=0;i<s->nb_streams;i++) {
442
        st = s->streams[i];
443
        if (st->id == startcode)
444
            goto found;
445
    }
446 e3d1cd8e Måns Rullgård
447
    es_type = m->psm_es_type[startcode & 0xff];
448 80e58c61 Benoit Fouet
    if(es_type > 0 && es_type != STREAM_TYPE_PRIVATE_DATA){
449 e3d1cd8e Måns Rullgård
        if(es_type == STREAM_TYPE_VIDEO_MPEG1){
450
            codec_id = CODEC_ID_MPEG2VIDEO;
451 72415b2a Stefano Sabatini
            type = AVMEDIA_TYPE_VIDEO;
452 e3d1cd8e Måns Rullgård
        } else if(es_type == STREAM_TYPE_VIDEO_MPEG2){
453
            codec_id = CODEC_ID_MPEG2VIDEO;
454 72415b2a Stefano Sabatini
            type = AVMEDIA_TYPE_VIDEO;
455 e3d1cd8e Måns Rullgård
        } else if(es_type == STREAM_TYPE_AUDIO_MPEG1 ||
456
                  es_type == STREAM_TYPE_AUDIO_MPEG2){
457
            codec_id = CODEC_ID_MP3;
458 72415b2a Stefano Sabatini
            type = AVMEDIA_TYPE_AUDIO;
459 e3d1cd8e Måns Rullgård
        } else if(es_type == STREAM_TYPE_AUDIO_AAC){
460
            codec_id = CODEC_ID_AAC;
461 72415b2a Stefano Sabatini
            type = AVMEDIA_TYPE_AUDIO;
462 e3d1cd8e Måns Rullgård
        } else if(es_type == STREAM_TYPE_VIDEO_MPEG4){
463
            codec_id = CODEC_ID_MPEG4;
464 72415b2a Stefano Sabatini
            type = AVMEDIA_TYPE_VIDEO;
465 e3d1cd8e Måns Rullgård
        } else if(es_type == STREAM_TYPE_VIDEO_H264){
466
            codec_id = CODEC_ID_H264;
467 72415b2a Stefano Sabatini
            type = AVMEDIA_TYPE_VIDEO;
468 e3d1cd8e Måns Rullgård
        } else if(es_type == STREAM_TYPE_AUDIO_AC3){
469
            codec_id = CODEC_ID_AC3;
470 72415b2a Stefano Sabatini
            type = AVMEDIA_TYPE_AUDIO;
471 e3d1cd8e Måns Rullgård
        } else {
472
            goto skip;
473
        }
474
    } else if (startcode >= 0x1e0 && startcode <= 0x1ef) {
475 83d07313 Måns Rullgård
        static const unsigned char avs_seqh[4] = { 0, 0, 1, 0xb0 };
476
        unsigned char buf[8];
477 b7effd4e Anton Khirnov
        avio_read(s->pb, buf, 8);
478 6b4aa5da Anton Khirnov
        avio_seek(s->pb, -8, SEEK_CUR);
479 83d07313 Måns Rullgård
        if(!memcmp(buf, avs_seqh, 4) && (buf[6] != 0 || buf[7] != 1))
480
            codec_id = CODEC_ID_CAVS;
481
        else
482 c1c90466 Michael Niedermayer
            codec_id = CODEC_ID_PROBE;
483 72415b2a Stefano Sabatini
        type = AVMEDIA_TYPE_VIDEO;
484 db7f1f95 Fabrice Bellard
    } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
485 72415b2a Stefano Sabatini
        type = AVMEDIA_TYPE_AUDIO;
486 2c187847 Måns Rullgård
        codec_id = m->sofdec > 0 ? CODEC_ID_ADPCM_ADX : CODEC_ID_MP2;
487 3f2bf07b Joakim Plate
    } else if (startcode >= 0x80 && startcode <= 0x87) {
488 72415b2a Stefano Sabatini
        type = AVMEDIA_TYPE_AUDIO;
489 db7f1f95 Fabrice Bellard
        codec_id = CODEC_ID_AC3;
490 274335e7 Michael Niedermayer
    } else if (  ( startcode >= 0x88 && startcode <= 0x8f)
491 aad512b4 Michael Niedermayer
               ||( startcode >= 0x98 && startcode <= 0x9f)) {
492
        /* 0x90 - 0x97 is reserved for SDDS in DVD specs */
493 72415b2a Stefano Sabatini
        type = AVMEDIA_TYPE_AUDIO;
494 23c99253 Michael Niedermayer
        codec_id = CODEC_ID_DTS;
495 aad512b4 Michael Niedermayer
    } else if (startcode >= 0xa0 && startcode <= 0xaf) {
496 72415b2a Stefano Sabatini
        type = AVMEDIA_TYPE_AUDIO;
497 4860abb1 Lars Täuber
        /* 16 bit form will be handled as CODEC_ID_PCM_S16BE */
498
        codec_id = CODEC_ID_PCM_DVD;
499 aad512b4 Michael Niedermayer
    } else if (startcode >= 0xb0 && startcode <= 0xbf) {
500 72415b2a Stefano Sabatini
        type = AVMEDIA_TYPE_AUDIO;
501 9ba4821d Ramiro Polla
        codec_id = CODEC_ID_TRUEHD;
502 aad512b4 Michael Niedermayer
    } else if (startcode >= 0xc0 && startcode <= 0xcf) {
503
        /* Used for both AC-3 and E-AC-3 in EVOB files */
504 72415b2a Stefano Sabatini
        type = AVMEDIA_TYPE_AUDIO;
505 aad512b4 Michael Niedermayer
        codec_id = CODEC_ID_AC3;
506 a9c3213f Fabrice Bellard
    } else if (startcode >= 0x20 && startcode <= 0x3f) {
507 72415b2a Stefano Sabatini
        type = AVMEDIA_TYPE_SUBTITLE;
508 a9c3213f Fabrice Bellard
        codec_id = CODEC_ID_DVD_SUBTITLE;
509 aad512b4 Michael Niedermayer
    } else if (startcode >= 0xfd55 && startcode <= 0xfd5f) {
510 72415b2a Stefano Sabatini
        type = AVMEDIA_TYPE_VIDEO;
511 aad512b4 Michael Niedermayer
        codec_id = CODEC_ID_VC1;
512 80e58c61 Benoit Fouet
    } else if (startcode == 0x1bd) {
513
        // check dvd audio substream type
514 72415b2a Stefano Sabatini
        type = AVMEDIA_TYPE_AUDIO;
515 80e58c61 Benoit Fouet
        switch(dvdaudio_substream_type & 0xe0) {
516
        case 0xa0:  codec_id = CODEC_ID_PCM_DVD;
517
                    break;
518
        case 0x80:  if((dvdaudio_substream_type & 0xf8) == 0x88)
519
                         codec_id = CODEC_ID_DTS;
520
                    else codec_id = CODEC_ID_AC3;
521
                    break;
522
        default:    av_log(s, AV_LOG_ERROR, "Unknown 0x1bd sub-stream\n");
523
                    goto skip;
524
        }
525 db7f1f95 Fabrice Bellard
    } else {
526
    skip:
527
        /* skip packet */
528 e356fc57 Anton Khirnov
        avio_seek(s->pb, len, SEEK_CUR);
529 db7f1f95 Fabrice Bellard
        goto redo;
530
    }
531 1e5c667c Fabrice Bellard
    /* no stream found: add a new stream */
532
    st = av_new_stream(s, startcode);
533 115329f1 Diego Biurrun
    if (!st)
534 1e5c667c Fabrice Bellard
        goto skip;
535 01f4895c Michael Niedermayer
    st->codec->codec_type = type;
536
    st->codec->codec_id = codec_id;
537 27f388aa Fabrice Bellard
    if (codec_id != CODEC_ID_PCM_S16BE)
538 57004ff1 Aurelien Jacobs
        st->need_parsing = AVSTREAM_PARSE_FULL;
539 de6d9b64 Fabrice Bellard
 found:
540 f3356e9c Michael Niedermayer
    if(st->discard >= AVDISCARD_ALL)
541 b9866ebc Michael Niedermayer
        goto skip;
542 80e58c61 Benoit Fouet
    if ((startcode >= 0xa0 && startcode <= 0xaf) ||
543
        (startcode == 0x1bd && ((dvdaudio_substream_type & 0xe0) == 0xa0))) {
544 9ec05e36 Fabrice Bellard
        int b1, freq;
545
546
        /* for LPCM, we just skip the header and consider it is raw
547
           audio data */
548
        if (len <= 3)
549
            goto skip;
550 b7effd4e Anton Khirnov
        avio_r8(s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
551
        b1 = avio_r8(s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
552
        avio_r8(s->pb); /* dynamic range control (0x80 = off) */
553 9ec05e36 Fabrice Bellard
        len -= 3;
554
        freq = (b1 >> 4) & 3;
555 01f4895c Michael Niedermayer
        st->codec->sample_rate = lpcm_freq_tab[freq];
556
        st->codec->channels = 1 + (b1 & 7);
557 dd1c8f3e Luca Abeni
        st->codec->bits_per_coded_sample = 16 + ((b1 >> 6) & 3) * 4;
558 4860abb1 Lars Täuber
        st->codec->bit_rate = st->codec->channels *
559
                              st->codec->sample_rate *
560 dd1c8f3e Luca Abeni
                              st->codec->bits_per_coded_sample;
561
        if (st->codec->bits_per_coded_sample == 16)
562 4860abb1 Lars Täuber
            st->codec->codec_id = CODEC_ID_PCM_S16BE;
563 dd1c8f3e Luca Abeni
        else if (st->codec->bits_per_coded_sample == 28)
564 4860abb1 Lars Täuber
            return AVERROR(EINVAL);
565 9ec05e36 Fabrice Bellard
    }
566 de6d9b64 Fabrice Bellard
    av_new_packet(pkt, len);
567 b7effd4e Anton Khirnov
    avio_read(s->pb, pkt->data, pkt->size);
568 de6d9b64 Fabrice Bellard
    pkt->pts = pts;
569 27f388aa Fabrice Bellard
    pkt->dts = dts;
570 c45ebd54 Michael Niedermayer
    pkt->pos = dummy_pos;
571 db7f1f95 Fabrice Bellard
    pkt->stream_index = st->index;
572 27a206e0 Michel Bardiaux
#if 0
573 b9866ebc Michael Niedermayer
    av_log(s, AV_LOG_DEBUG, "%d: pts=%0.3f dts=%0.3f size=%d\n",
574
           pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0, pkt->size);
575 27a206e0 Michel Bardiaux
#endif
576 0bd586c5 Mike Melanson
577 de6d9b64 Fabrice Bellard
    return 0;
578
}
579
580 115329f1 Diego Biurrun
static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
581 8d14a25c Michael Niedermayer
                               int64_t *ppos, int64_t pos_limit)
582 27f388aa Fabrice Bellard
{
583
    int len, startcode;
584
    int64_t pos, pts, dts;
585
586
    pos = *ppos;
587
#ifdef DEBUG_SEEK
588 949b1a13 Steve L'Homme
    printf("read_dts: pos=0x%"PRIx64" next=%d -> ", pos, find_next);
589 27f388aa Fabrice Bellard
#endif
590 6b4aa5da Anton Khirnov
    if (avio_seek(s->pb, pos, SEEK_SET) < 0)
591 5faf1689 Joakim Plate
        return AV_NOPTS_VALUE;
592
593 27f388aa Fabrice Bellard
    for(;;) {
594 8d14a25c Michael Niedermayer
        len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts);
595 27f388aa Fabrice Bellard
        if (len < 0) {
596
#ifdef DEBUG_SEEK
597
            printf("none (ret=%d)\n", len);
598
#endif
599
            return AV_NOPTS_VALUE;
600
        }
601 115329f1 Diego Biurrun
        if (startcode == s->streams[stream_index]->id &&
602 27f388aa Fabrice Bellard
            dts != AV_NOPTS_VALUE) {
603
            break;
604
        }
605 e356fc57 Anton Khirnov
        avio_seek(s->pb, len, SEEK_CUR);
606 27f388aa Fabrice Bellard
    }
607
#ifdef DEBUG_SEEK
608 949b1a13 Steve L'Homme
    printf("pos=0x%"PRIx64" dts=0x%"PRIx64" %0.3f\n", pos, dts, dts / 90000.0);
609 27f388aa Fabrice Bellard
#endif
610
    *ppos = pos;
611 cdd5034f Michael Niedermayer
    return dts;
612 27f388aa Fabrice Bellard
}
613
614 c6610a21 Diego Elio Pettenò
AVInputFormat ff_mpegps_demuxer = {
615 db7f1f95 Fabrice Bellard
    "mpeg",
616 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("MPEG-PS format"),
617 db7f1f95 Fabrice Bellard
    sizeof(MpegDemuxContext),
618
    mpegps_probe,
619
    mpegps_read_header,
620
    mpegps_read_packet,
621 9b64a036 Baptiste Coudurier
    NULL,
622 8d14a25c Michael Niedermayer
    NULL, //mpegps_read_seek,
623
    mpegps_read_dts,
624 ff9c8d7c Michael Niedermayer
    .flags = AVFMT_SHOW_IDS|AVFMT_TS_DISCONT,
625 de6d9b64 Fabrice Bellard
};