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