Statistics
| Branch: | Revision:

ffmpeg / libavformat / mpeg.c @ 5aa0a644

History | View | Annotate | Download (19.7 KB)

1 de6d9b64 Fabrice Bellard
/*
2 fb7566d0 Fabrice Bellard
 * MPEG1/2 mux/demux
3 19720f15 Fabrice Bellard
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
4 de6d9b64 Fabrice Bellard
 *
5 19720f15 Fabrice Bellard
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9 de6d9b64 Fabrice Bellard
 *
10 19720f15 Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
11 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14 de6d9b64 Fabrice Bellard
 *
15 19720f15 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 de6d9b64 Fabrice Bellard
 */
19
#include "avformat.h"
20
21
#define MAX_PAYLOAD_SIZE 4096
22
#define NB_STREAMS 2
23
24
typedef struct {
25 0c1a9eda Zdenek Kabelac
    uint8_t buffer[MAX_PAYLOAD_SIZE];
26 de6d9b64 Fabrice Bellard
    int buffer_ptr;
27 0c1a9eda Zdenek Kabelac
    uint8_t id;
28 de6d9b64 Fabrice Bellard
    int max_buffer_size; /* in bytes */
29
    int packet_number;
30 0c1a9eda Zdenek Kabelac
    int64_t start_pts;
31 de6d9b64 Fabrice Bellard
} StreamInfo;
32
33
typedef struct {
34
    int packet_size; /* required packet size */
35
    int packet_data_max_size; /* maximum data size inside a packet */
36
    int packet_number;
37
    int pack_header_freq;     /* frequency (in packets^-1) at which we send pack headers */
38
    int system_header_freq;
39
    int mux_rate; /* bitrate in units of 50 bytes/s */
40
    /* stream info */
41
    int audio_bound;
42
    int video_bound;
43 fb7566d0 Fabrice Bellard
    int is_mpeg2;
44
    int is_vcd;
45 de6d9b64 Fabrice Bellard
} MpegMuxContext;
46
47
#define PACK_START_CODE             ((unsigned int)0x000001ba)
48
#define SYSTEM_HEADER_START_CODE    ((unsigned int)0x000001bb)
49 92b3e125 Juanjo
#define SEQUENCE_END_CODE           ((unsigned int)0x000001b7)
50 de6d9b64 Fabrice Bellard
#define PACKET_START_CODE_MASK      ((unsigned int)0xffffff00)
51
#define PACKET_START_CODE_PREFIX    ((unsigned int)0x00000100)
52
#define ISO_11172_END_CODE          ((unsigned int)0x000001b9)
53
  
54
/* mpeg2 */
55
#define PROGRAM_STREAM_MAP 0x1bc
56
#define PRIVATE_STREAM_1   0x1bd
57
#define PADDING_STREAM     0x1be
58
#define PRIVATE_STREAM_2   0x1bf
59
60
61
#define AUDIO_ID 0xc0
62
#define VIDEO_ID 0xe0
63
64 fb7566d0 Fabrice Bellard
extern AVOutputFormat mpeg1system_mux;
65
extern AVOutputFormat mpeg1vcd_mux;
66
extern AVOutputFormat mpeg2vob_mux;
67
68 de6d9b64 Fabrice Bellard
static int put_pack_header(AVFormatContext *ctx, 
69 0c1a9eda Zdenek Kabelac
                           uint8_t *buf, int64_t timestamp)
70 de6d9b64 Fabrice Bellard
{
71
    MpegMuxContext *s = ctx->priv_data;
72
    PutBitContext pb;
73
    
74
    init_put_bits(&pb, buf, 128, NULL, NULL);
75
76
    put_bits(&pb, 32, PACK_START_CODE);
77 b2cac184 Fabrice Bellard
    if (s->is_mpeg2) {
78 8683e4a0 Måns Rullgård
        put_bits(&pb, 2, 0x1);
79 b2cac184 Fabrice Bellard
    } else {
80
        put_bits(&pb, 4, 0x2);
81
    }
82 0c1a9eda Zdenek Kabelac
    put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
83 de6d9b64 Fabrice Bellard
    put_bits(&pb, 1, 1);
84 0c1a9eda Zdenek Kabelac
    put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
85 de6d9b64 Fabrice Bellard
    put_bits(&pb, 1, 1);
86 0c1a9eda Zdenek Kabelac
    put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
87 de6d9b64 Fabrice Bellard
    put_bits(&pb, 1, 1);
88 b2cac184 Fabrice Bellard
    if (s->is_mpeg2) {
89
        /* clock extension */
90
        put_bits(&pb, 9, 0);
91
        put_bits(&pb, 1, 1);
92
    }
93 de6d9b64 Fabrice Bellard
    put_bits(&pb, 1, 1);
94
    put_bits(&pb, 22, s->mux_rate);
95
    put_bits(&pb, 1, 1);
96 b2cac184 Fabrice Bellard
    if (s->is_mpeg2) {
97
        put_bits(&pb, 5, 0x1f); /* reserved */
98
        put_bits(&pb, 3, 0); /* stuffing length */
99
    }
100 de6d9b64 Fabrice Bellard
    flush_put_bits(&pb);
101 17592475 Michael Niedermayer
    return pbBufPtr(&pb) - pb.buf;
102 de6d9b64 Fabrice Bellard
}
103
104 0c1a9eda Zdenek Kabelac
static int put_system_header(AVFormatContext *ctx, uint8_t *buf)
105 de6d9b64 Fabrice Bellard
{
106
    MpegMuxContext *s = ctx->priv_data;
107
    int size, rate_bound, i, private_stream_coded, id;
108
    PutBitContext pb;
109
110
    init_put_bits(&pb, buf, 128, NULL, NULL);
111
112
    put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
113
    put_bits(&pb, 16, 0);
114
    put_bits(&pb, 1, 1);
115
    
116
    rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */
117
    put_bits(&pb, 22, rate_bound);
118
    put_bits(&pb, 1, 1); /* marker */
119
    put_bits(&pb, 6, s->audio_bound);
120
121
    put_bits(&pb, 1, 1); /* variable bitrate */
122
    put_bits(&pb, 1, 1); /* non constrainted bit stream */
123
    
124
    put_bits(&pb, 1, 0); /* audio locked */
125
    put_bits(&pb, 1, 0); /* video locked */
126
    put_bits(&pb, 1, 1); /* marker */
127
128
    put_bits(&pb, 5, s->video_bound);
129
    put_bits(&pb, 8, 0xff); /* reserved byte */
130
    
131
    /* audio stream info */
132
    private_stream_coded = 0;
133
    for(i=0;i<ctx->nb_streams;i++) {
134
        StreamInfo *stream = ctx->streams[i]->priv_data;
135
        id = stream->id;
136
        if (id < 0xc0) {
137
            /* special case for private streams (AC3 use that) */
138
            if (private_stream_coded)
139
                continue;
140
            private_stream_coded = 1;
141
            id = 0xbd;
142
        }
143
        put_bits(&pb, 8, id); /* stream ID */
144
        put_bits(&pb, 2, 3);
145
        if (id < 0xe0) {
146
            /* audio */
147
            put_bits(&pb, 1, 0);
148
            put_bits(&pb, 13, stream->max_buffer_size / 128);
149
        } else {
150
            /* video */
151
            put_bits(&pb, 1, 1);
152
            put_bits(&pb, 13, stream->max_buffer_size / 1024);
153
        }
154
    }
155
    flush_put_bits(&pb);
156 17592475 Michael Niedermayer
    size = pbBufPtr(&pb) - pb.buf;
157 de6d9b64 Fabrice Bellard
    /* patch packet size */
158
    buf[4] = (size - 6) >> 8;
159
    buf[5] = (size - 6) & 0xff;
160
161
    return size;
162
}
163
164
static int mpeg_mux_init(AVFormatContext *ctx)
165
{
166 db7f1f95 Fabrice Bellard
    MpegMuxContext *s = ctx->priv_data;
167 de6d9b64 Fabrice Bellard
    int bitrate, i, mpa_id, mpv_id, ac3_id;
168
    AVStream *st;
169
    StreamInfo *stream;
170
171
    s->packet_number = 0;
172 fb7566d0 Fabrice Bellard
    s->is_vcd = (ctx->oformat == &mpeg1vcd_mux);
173
    s->is_mpeg2 = (ctx->oformat == &mpeg2vob_mux);
174
    
175
    if (s->is_vcd)
176 92b3e125 Juanjo
        s->packet_size = 2324; /* VCD packet size */
177
    else
178
        s->packet_size = 2048;
179
        
180 de6d9b64 Fabrice Bellard
    /* startcode(4) + length(2) + flags(1) */
181
    s->packet_data_max_size = s->packet_size - 7;
182
    s->audio_bound = 0;
183
    s->video_bound = 0;
184
    mpa_id = AUDIO_ID;
185
    ac3_id = 0x80;
186
    mpv_id = VIDEO_ID;
187
    for(i=0;i<ctx->nb_streams;i++) {
188
        st = ctx->streams[i];
189
        stream = av_mallocz(sizeof(StreamInfo));
190
        if (!stream)
191
            goto fail;
192
        st->priv_data = stream;
193
194
        switch(st->codec.codec_type) {
195
        case CODEC_TYPE_AUDIO:
196
            if (st->codec.codec_id == CODEC_ID_AC3)
197
                stream->id = ac3_id++;
198
            else
199
                stream->id = mpa_id++;
200
            stream->max_buffer_size = 4 * 1024; 
201
            s->audio_bound++;
202
            break;
203
        case CODEC_TYPE_VIDEO:
204
            stream->id = mpv_id++;
205
            stream->max_buffer_size = 46 * 1024; 
206
            s->video_bound++;
207
            break;
208 ac5e6a5b Philip Gladstone
        default:
209 42343f7e Philip Gladstone
            av_abort();
210 de6d9b64 Fabrice Bellard
        }
211
    }
212
213
    /* we increase slightly the bitrate to take into account the
214
       headers. XXX: compute it exactly */
215
    bitrate = 2000;
216
    for(i=0;i<ctx->nb_streams;i++) {
217
        st = ctx->streams[i];
218
        bitrate += st->codec.bit_rate;
219
    }
220
    s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
221 92b3e125 Juanjo
    
222 fb7566d0 Fabrice Bellard
    if (s->is_vcd || s->is_mpeg2)
223 92b3e125 Juanjo
        /* every packet */
224
        s->pack_header_freq = 1;
225
    else
226
        /* every 2 seconds */
227
        s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
228
    
229 b2cac184 Fabrice Bellard
    if (s->is_mpeg2)
230
        /* every 200 packets. Need to look at the spec.  */
231
        s->system_header_freq = s->pack_header_freq * 40;
232
    else if (s->is_vcd)
233 92b3e125 Juanjo
        /* every 40 packets, this is my invention */
234
        s->system_header_freq = s->pack_header_freq * 40;
235
    else
236
        s->system_header_freq = s->pack_header_freq * 5;
237
    
238 de6d9b64 Fabrice Bellard
    for(i=0;i<ctx->nb_streams;i++) {
239
        stream = ctx->streams[i]->priv_data;
240
        stream->buffer_ptr = 0;
241
        stream->packet_number = 0;
242
        stream->start_pts = -1;
243
    }
244
    return 0;
245
 fail:
246
    for(i=0;i<ctx->nb_streams;i++) {
247 1ea4f593 Fabrice Bellard
        av_free(ctx->streams[i]->priv_data);
248 de6d9b64 Fabrice Bellard
    }
249
    return -ENOMEM;
250
}
251
252
/* flush the packet on stream stream_index */
253 fa0f62c3 Fabrice Bellard
static void flush_packet(AVFormatContext *ctx, int stream_index)
254 de6d9b64 Fabrice Bellard
{
255
    MpegMuxContext *s = ctx->priv_data;
256
    StreamInfo *stream = ctx->streams[stream_index]->priv_data;
257 0c1a9eda Zdenek Kabelac
    uint8_t *buf_ptr;
258 fb7566d0 Fabrice Bellard
    int size, payload_size, startcode, id, len, stuffing_size, i, header_len;
259 0c1a9eda Zdenek Kabelac
    int64_t timestamp;
260
    uint8_t buffer[128];
261 92b3e125 Juanjo
    
262 de6d9b64 Fabrice Bellard
    id = stream->id;
263
    timestamp = stream->start_pts;
264
265
#if 0
266
    printf("packet ID=%2x PTS=%0.3f\n", 
267
           id, timestamp / 90000.0);
268
#endif
269
270
    buf_ptr = buffer;
271 92b3e125 Juanjo
    if (((s->packet_number % s->pack_header_freq) == 0)) {
272 de6d9b64 Fabrice Bellard
        /* output pack and systems header if needed */
273
        size = put_pack_header(ctx, buf_ptr, timestamp);
274
        buf_ptr += size;
275
        if ((s->packet_number % s->system_header_freq) == 0) {
276
            size = put_system_header(ctx, buf_ptr);
277
            buf_ptr += size;
278
        }
279
    }
280
    size = buf_ptr - buffer;
281
    put_buffer(&ctx->pb, buffer, size);
282
283
    /* packet header */
284 fb7566d0 Fabrice Bellard
    if (s->is_mpeg2) {
285
        header_len = 8;
286
    } else {
287
        header_len = 5;
288
    }
289 fa0f62c3 Fabrice Bellard
    payload_size = s->packet_size - (size + 6 + header_len);
290 de6d9b64 Fabrice Bellard
    if (id < 0xc0) {
291
        startcode = PRIVATE_STREAM_1;
292
        payload_size -= 4;
293
    } else {
294
        startcode = 0x100 + id;
295
    }
296
    stuffing_size = payload_size - stream->buffer_ptr;
297
    if (stuffing_size < 0)
298
        stuffing_size = 0;
299
300
    put_be32(&ctx->pb, startcode);
301
302 fb7566d0 Fabrice Bellard
    put_be16(&ctx->pb, payload_size + header_len);
303 de6d9b64 Fabrice Bellard
    /* stuffing */
304
    for(i=0;i<stuffing_size;i++)
305
        put_byte(&ctx->pb, 0xff);
306 fb7566d0 Fabrice Bellard
307
    if (s->is_mpeg2) {
308
        put_byte(&ctx->pb, 0x80); /* mpeg2 id */
309
        put_byte(&ctx->pb, 0x80); /* flags */
310
        put_byte(&ctx->pb, 0x05); /* header len (only pts is included) */
311
    }
312 de6d9b64 Fabrice Bellard
    put_byte(&ctx->pb, 
313
             (0x02 << 4) | 
314
             (((timestamp >> 30) & 0x07) << 1) | 
315
             1);
316 0c1a9eda Zdenek Kabelac
    put_be16(&ctx->pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
317
    put_be16(&ctx->pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
318 de6d9b64 Fabrice Bellard
319
    if (startcode == PRIVATE_STREAM_1) {
320
        put_byte(&ctx->pb, id);
321
        if (id >= 0x80 && id <= 0xbf) {
322
            /* XXX: need to check AC3 spec */
323
            put_byte(&ctx->pb, 1);
324
            put_byte(&ctx->pb, 0);
325
            put_byte(&ctx->pb, 2);
326
        }
327
    }
328
329
    /* output data */
330
    put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size);
331
    put_flush_packet(&ctx->pb);
332
    
333
    /* preserve remaining data */
334
    len = stream->buffer_ptr - payload_size;
335
    if (len < 0) 
336
        len = 0;
337
    memmove(stream->buffer, stream->buffer + stream->buffer_ptr - len, len);
338
    stream->buffer_ptr = len;
339
340
    s->packet_number++;
341
    stream->packet_number++;
342
    stream->start_pts = -1;
343
}
344
345 10bb7023 Juanjo
static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index,
346 49057904 Fabrice Bellard
                                 const uint8_t *buf, int size, int64_t pts)
347 de6d9b64 Fabrice Bellard
{
348
    MpegMuxContext *s = ctx->priv_data;
349
    AVStream *st = ctx->streams[stream_index];
350
    StreamInfo *stream = st->priv_data;
351
    int len;
352 10bb7023 Juanjo
    
353 de6d9b64 Fabrice Bellard
    while (size > 0) {
354
        /* set pts */
355 10bb7023 Juanjo
        if (stream->start_pts == -1) {
356 b2cac184 Fabrice Bellard
            stream->start_pts = pts;
357 10bb7023 Juanjo
        }
358 de6d9b64 Fabrice Bellard
        len = s->packet_data_max_size - stream->buffer_ptr;
359
        if (len > size)
360
            len = size;
361
        memcpy(stream->buffer + stream->buffer_ptr, buf, len);
362
        stream->buffer_ptr += len;
363
        buf += len;
364
        size -= len;
365
        while (stream->buffer_ptr >= s->packet_data_max_size) {
366
            /* output the packet */
367
            if (stream->start_pts == -1)
368 b2cac184 Fabrice Bellard
                stream->start_pts = pts;
369 fa0f62c3 Fabrice Bellard
            flush_packet(ctx, stream_index);
370 de6d9b64 Fabrice Bellard
        }
371
    }
372
    return 0;
373
}
374
375
static int mpeg_mux_end(AVFormatContext *ctx)
376
{
377
    StreamInfo *stream;
378
    int i;
379
380
    /* flush each packet */
381
    for(i=0;i<ctx->nb_streams;i++) {
382
        stream = ctx->streams[i]->priv_data;
383 92b3e125 Juanjo
        if (stream->buffer_ptr > 0) {
384 fa0f62c3 Fabrice Bellard
            flush_packet(ctx, i);
385 92b3e125 Juanjo
        }
386 de6d9b64 Fabrice Bellard
    }
387
388 fa0f62c3 Fabrice Bellard
    /* End header according to MPEG1 systems standard. We do not write
389
       it as it is usually not needed by decoders and because it
390
       complicates MPEG stream concatenation. */
391 92b3e125 Juanjo
    //put_be32(&ctx->pb, ISO_11172_END_CODE);
392
    //put_flush_packet(&ctx->pb);
393 9d90c37f Michael Niedermayer
394
    for(i=0;i<ctx->nb_streams;i++)
395
        av_freep(&ctx->streams[i]->priv_data);
396
397 de6d9b64 Fabrice Bellard
    return 0;
398
}
399
400
/*********************************************/
401
/* demux code */
402
403
#define MAX_SYNC_SIZE 100000
404
405 db7f1f95 Fabrice Bellard
static int mpegps_probe(AVProbeData *p)
406
{
407 ec23a472 Isaac Richards
    int code, c, i;
408 db7f1f95 Fabrice Bellard
409 ec23a472 Isaac Richards
    code = 0xff;
410 db7f1f95 Fabrice Bellard
    /* we search the first start code. If it is a packet start code,
411
       then we decide it is mpeg ps. We do not send highest value to
412
       give a chance to mpegts */
413 fa777321 Fabrice Bellard
    /* NOTE: the search range was restricted to avoid too many false
414
       detections */
415
416
    if (p->buf_size < 6)
417
        return 0;
418 ec23a472 Isaac Richards
419
    for (i = 0; i < 20; i++) {
420
        c = p->buf[i];
421
        code = (code << 8) | c;
422
        if ((code & 0xffffff00) == 0x100) {
423
            if (code == PACK_START_CODE ||
424
                code == SYSTEM_HEADER_START_CODE ||
425
                (code >= 0x1e0 && code <= 0x1ef) ||
426
                (code >= 0x1c0 && code <= 0x1df) ||
427
                code == PRIVATE_STREAM_2 ||
428
                code == PROGRAM_STREAM_MAP ||
429
                code == PRIVATE_STREAM_1 ||
430
                code == PADDING_STREAM)
431 149f7c02 Michael Niedermayer
                return AVPROBE_SCORE_MAX - 2;
432 ec23a472 Isaac Richards
            else
433
                return 0;
434
        }
435 db7f1f95 Fabrice Bellard
    }
436
    return 0;
437
}
438
439
440 de6d9b64 Fabrice Bellard
typedef struct MpegDemuxContext {
441
    int header_state;
442
} MpegDemuxContext;
443
444
static int find_start_code(ByteIOContext *pb, int *size_ptr, 
445 0c1a9eda Zdenek Kabelac
                           uint32_t *header_state)
446 de6d9b64 Fabrice Bellard
{
447
    unsigned int state, v;
448
    int val, n;
449
450
    state = *header_state;
451
    n = *size_ptr;
452
    while (n > 0) {
453
        if (url_feof(pb))
454
            break;
455
        v = get_byte(pb);
456
        n--;
457
        if (state == 0x000001) {
458
            state = ((state << 8) | v) & 0xffffff;
459
            val = state;
460
            goto found;
461
        }
462
        state = ((state << 8) | v) & 0xffffff;
463
    }
464
    val = -1;
465
 found:
466
    *header_state = state;
467
    *size_ptr = n;
468
    return val;
469
}
470
471 db7f1f95 Fabrice Bellard
static int mpegps_read_header(AVFormatContext *s,
472
                                  AVFormatParameters *ap)
473 de6d9b64 Fabrice Bellard
{
474 db7f1f95 Fabrice Bellard
    MpegDemuxContext *m = s->priv_data;
475 de6d9b64 Fabrice Bellard
    m->header_state = 0xff;
476 db7f1f95 Fabrice Bellard
    /* no need to do more */
477 de6d9b64 Fabrice Bellard
    return 0;
478
}
479
480 0c1a9eda Zdenek Kabelac
static int64_t get_pts(ByteIOContext *pb, int c)
481 de6d9b64 Fabrice Bellard
{
482 0c1a9eda Zdenek Kabelac
    int64_t pts;
483 de6d9b64 Fabrice Bellard
    int val;
484
485
    if (c < 0)
486
        c = get_byte(pb);
487 0c1a9eda Zdenek Kabelac
    pts = (int64_t)((c >> 1) & 0x07) << 30;
488 de6d9b64 Fabrice Bellard
    val = get_be16(pb);
489 0c1a9eda Zdenek Kabelac
    pts |= (int64_t)(val >> 1) << 15;
490 de6d9b64 Fabrice Bellard
    val = get_be16(pb);
491 0c1a9eda Zdenek Kabelac
    pts |= (int64_t)(val >> 1);
492 de6d9b64 Fabrice Bellard
    return pts;
493
}
494
495 db7f1f95 Fabrice Bellard
static int mpegps_read_packet(AVFormatContext *s,
496
                                  AVPacket *pkt)
497 de6d9b64 Fabrice Bellard
{
498
    MpegDemuxContext *m = s->priv_data;
499
    AVStream *st;
500 db7f1f95 Fabrice Bellard
    int len, size, startcode, i, c, flags, header_len, type, codec_id;
501 0c1a9eda Zdenek Kabelac
    int64_t pts, dts;
502 de6d9b64 Fabrice Bellard
503 b2cac184 Fabrice Bellard
    /* next start code (should be immediately after) */
504 de6d9b64 Fabrice Bellard
 redo:
505
    m->header_state = 0xff;
506
    size = MAX_SYNC_SIZE;
507
    startcode = find_start_code(&s->pb, &size, &m->header_state);
508 001e3f55 Juanjo
    //printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
509 de6d9b64 Fabrice Bellard
    if (startcode < 0)
510
        return -EIO;
511
    if (startcode == PACK_START_CODE)
512
        goto redo;
513
    if (startcode == SYSTEM_HEADER_START_CODE)
514
        goto redo;
515
    if (startcode == PADDING_STREAM ||
516
        startcode == PRIVATE_STREAM_2) {
517
        /* skip them */
518
        len = get_be16(&s->pb);
519
        url_fskip(&s->pb, len);
520
        goto redo;
521
    }
522
    /* find matching stream */
523
    if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
524
          (startcode >= 0x1e0 && startcode <= 0x1ef) ||
525
          (startcode == 0x1bd)))
526
        goto redo;
527
528
    len = get_be16(&s->pb);
529 b2cac184 Fabrice Bellard
    pts = AV_NOPTS_VALUE;
530
    dts = AV_NOPTS_VALUE;
531 de6d9b64 Fabrice Bellard
    /* stuffing */
532
    for(;;) {
533
        c = get_byte(&s->pb);
534
        len--;
535
        /* XXX: for mpeg1, should test only bit 7 */
536
        if (c != 0xff) 
537
            break;
538
    }
539
    if ((c & 0xc0) == 0x40) {
540
        /* buffer scale & size */
541
        get_byte(&s->pb);
542
        c = get_byte(&s->pb);
543
        len -= 2;
544
    }
545
    if ((c & 0xf0) == 0x20) {
546
        pts = get_pts(&s->pb, c);
547
        len -= 4;
548
    } else if ((c & 0xf0) == 0x30) {
549
        pts = get_pts(&s->pb, c);
550
        dts = get_pts(&s->pb, -1);
551
        len -= 9;
552
    } else if ((c & 0xc0) == 0x80) {
553
        /* mpeg 2 PES */
554
        if ((c & 0x30) != 0) {
555
            fprintf(stderr, "Encrypted multiplex not handled\n");
556
            return -EIO;
557
        }
558
        flags = get_byte(&s->pb);
559
        header_len = get_byte(&s->pb);
560
        len -= 2;
561
        if (header_len > len)
562
            goto redo;
563 1e5c667c Fabrice Bellard
        if ((flags & 0xc0) == 0x80) {
564 de6d9b64 Fabrice Bellard
            pts = get_pts(&s->pb, -1);
565
            header_len -= 5;
566
            len -= 5;
567
        } if ((flags & 0xc0) == 0xc0) {
568
            pts = get_pts(&s->pb, -1);
569
            dts = get_pts(&s->pb, -1);
570
            header_len -= 10;
571
            len -= 10;
572
        }
573
        len -= header_len;
574
        while (header_len > 0) {
575
            get_byte(&s->pb);
576
            header_len--;
577
        }
578
    }
579
    if (startcode == 0x1bd) {
580
        startcode = get_byte(&s->pb);
581
        len--;
582
        if (startcode >= 0x80 && startcode <= 0xbf) {
583
            /* audio: skip header */
584
            get_byte(&s->pb);
585
            get_byte(&s->pb);
586
            get_byte(&s->pb);
587
            len -= 3;
588
        }
589
    }
590
591
    /* now find stream */
592
    for(i=0;i<s->nb_streams;i++) {
593
        st = s->streams[i];
594
        if (st->id == startcode)
595
            goto found;
596
    }
597 db7f1f95 Fabrice Bellard
    if (startcode >= 0x1e0 && startcode <= 0x1ef) {
598
        type = CODEC_TYPE_VIDEO;
599
        codec_id = CODEC_ID_MPEG1VIDEO;
600
    } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
601
        type = CODEC_TYPE_AUDIO;
602
        codec_id = CODEC_ID_MP2;
603
    } else if (startcode >= 0x80 && startcode <= 0x9f) {
604
        type = CODEC_TYPE_AUDIO;
605
        codec_id = CODEC_ID_AC3;
606 9ec05e36 Fabrice Bellard
    } else if (startcode >= 0xa0 && startcode <= 0xbf) {
607
        type = CODEC_TYPE_AUDIO;
608
        codec_id = CODEC_ID_PCM_S16BE;
609 db7f1f95 Fabrice Bellard
    } else {
610
    skip:
611
        /* skip packet */
612
        url_fskip(&s->pb, len);
613
        goto redo;
614
    }
615 1e5c667c Fabrice Bellard
    /* no stream found: add a new stream */
616
    st = av_new_stream(s, startcode);
617
    if (!st) 
618
        goto skip;
619 db7f1f95 Fabrice Bellard
    st->codec.codec_type = type;
620
    st->codec.codec_id = codec_id;
621 de6d9b64 Fabrice Bellard
 found:
622 9ec05e36 Fabrice Bellard
    if (startcode >= 0xa0 && startcode <= 0xbf) {
623
        int b1, freq;
624
        static const int lpcm_freq_tab[4] = { 48000, 96000, 44100, 32000 };
625
626
        /* for LPCM, we just skip the header and consider it is raw
627
           audio data */
628
        if (len <= 3)
629
            goto skip;
630
        get_byte(&s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
631
        b1 = get_byte(&s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
632
        get_byte(&s->pb); /* dynamic range control (0x80 = off) */
633
        len -= 3;
634
        freq = (b1 >> 4) & 3;
635
        st->codec.sample_rate = lpcm_freq_tab[freq];
636
        st->codec.channels = 1 + (b1 & 7);
637
        st->codec.bit_rate = st->codec.channels * st->codec.sample_rate * 2;
638
    }
639 de6d9b64 Fabrice Bellard
    av_new_packet(pkt, len);
640 10bb7023 Juanjo
    //printf("\nRead Packet ID: %x PTS: %f Size: %d", startcode,
641
    //       (float)pts/90000, len);
642 de6d9b64 Fabrice Bellard
    get_buffer(&s->pb, pkt->data, pkt->size);
643
    pkt->pts = pts;
644 db7f1f95 Fabrice Bellard
    pkt->stream_index = st->index;
645 de6d9b64 Fabrice Bellard
    return 0;
646
}
647
648 db7f1f95 Fabrice Bellard
static int mpegps_read_close(AVFormatContext *s)
649 de6d9b64 Fabrice Bellard
{
650
    return 0;
651
}
652
653 fb7566d0 Fabrice Bellard
static AVOutputFormat mpeg1system_mux = {
654 de6d9b64 Fabrice Bellard
    "mpeg",
655 fb7566d0 Fabrice Bellard
    "MPEG1 System format",
656 c6c11cb6 Ryutaroh Matsumoto
    "video/mpeg",
657 fb7566d0 Fabrice Bellard
    "mpg,mpeg",
658
    sizeof(MpegMuxContext),
659
    CODEC_ID_MP2,
660
    CODEC_ID_MPEG1VIDEO,
661
    mpeg_mux_init,
662
    mpeg_mux_write_packet,
663
    mpeg_mux_end,
664
};
665
666
static AVOutputFormat mpeg1vcd_mux = {
667
    "vcd",
668
    "MPEG1 System format (VCD)",
669 c6c11cb6 Ryutaroh Matsumoto
    "video/mpeg",
670 fb7566d0 Fabrice Bellard
    NULL,
671
    sizeof(MpegMuxContext),
672
    CODEC_ID_MP2,
673
    CODEC_ID_MPEG1VIDEO,
674
    mpeg_mux_init,
675
    mpeg_mux_write_packet,
676
    mpeg_mux_end,
677
};
678
679
static AVOutputFormat mpeg2vob_mux = {
680
    "vob",
681
    "MPEG2 PS format (VOB)",
682 c6c11cb6 Ryutaroh Matsumoto
    "video/mpeg",
683 fb7566d0 Fabrice Bellard
    "vob",
684 db7f1f95 Fabrice Bellard
    sizeof(MpegMuxContext),
685 de6d9b64 Fabrice Bellard
    CODEC_ID_MP2,
686
    CODEC_ID_MPEG1VIDEO,
687
    mpeg_mux_init,
688
    mpeg_mux_write_packet,
689
    mpeg_mux_end,
690 db7f1f95 Fabrice Bellard
};
691 de6d9b64 Fabrice Bellard
692 32f38cb4 Fabrice Bellard
AVInputFormat mpegps_demux = {
693 db7f1f95 Fabrice Bellard
    "mpeg",
694
    "MPEG PS format",
695
    sizeof(MpegDemuxContext),
696
    mpegps_probe,
697
    mpegps_read_header,
698
    mpegps_read_packet,
699
    mpegps_read_close,
700 bb76a117 Måns Rullgård
    .flags = AVFMT_NOHEADER,
701 de6d9b64 Fabrice Bellard
};
702 db7f1f95 Fabrice Bellard
703
int mpegps_init(void)
704
{
705 fb7566d0 Fabrice Bellard
    av_register_output_format(&mpeg1system_mux);
706
    av_register_output_format(&mpeg1vcd_mux);
707
    av_register_output_format(&mpeg2vob_mux);
708 db7f1f95 Fabrice Bellard
    av_register_input_format(&mpegps_demux);
709
    return 0;
710
}