- Fix pts calculation on mpeg mux (A/V sync) - Thanks to Lennert Buytenhek
[libav.git] / libav / mpeg.c
1 /*
2 * Output a MPEG1 multiplexed video/audio stream
3 * Copyright (c) 2000 Gerard Lantau.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 #include "avformat.h"
20 #include "tick.h"
21
22 #define MAX_PAYLOAD_SIZE 4096
23 #define NB_STREAMS 2
24
25 typedef struct {
26 UINT8 buffer[MAX_PAYLOAD_SIZE];
27 int buffer_ptr;
28 UINT8 id;
29 int max_buffer_size; /* in bytes */
30 int packet_number;
31 INT64 pts;
32 Ticker pts_ticker;
33 INT64 start_pts;
34 } StreamInfo;
35
36 typedef struct {
37 int packet_size; /* required packet size */
38 int packet_data_max_size; /* maximum data size inside a packet */
39 int packet_number;
40 int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
41 int system_header_freq;
42 int mux_rate; /* bitrate in units of 50 bytes/s */
43 /* stream info */
44 int audio_bound;
45 int video_bound;
46 } MpegMuxContext;
47
48 #define PACK_START_CODE ((unsigned int)0x000001ba)
49 #define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb)
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
64 static int mpeg_mux_check_packet(AVFormatContext *s, int *size);
65
66 static int put_pack_header(AVFormatContext *ctx,
67 UINT8 *buf, INT64 timestamp)
68 {
69 MpegMuxContext *s = ctx->priv_data;
70 PutBitContext pb;
71
72 init_put_bits(&pb, buf, 128, NULL, NULL);
73
74 put_bits(&pb, 32, PACK_START_CODE);
75 put_bits(&pb, 4, 0x2);
76 put_bits(&pb, 3, (UINT32)((timestamp >> 30) & 0x07));
77 put_bits(&pb, 1, 1);
78 put_bits(&pb, 15, (UINT32)((timestamp >> 15) & 0x7fff));
79 put_bits(&pb, 1, 1);
80 put_bits(&pb, 15, (UINT32)((timestamp) & 0x7fff));
81 put_bits(&pb, 1, 1);
82 put_bits(&pb, 1, 1);
83 put_bits(&pb, 22, s->mux_rate);
84 put_bits(&pb, 1, 1);
85
86 flush_put_bits(&pb);
87 return pbBufPtr(&pb) - pb.buf;
88 }
89
90 static int put_system_header(AVFormatContext *ctx, UINT8 *buf)
91 {
92 MpegMuxContext *s = ctx->priv_data;
93 int size, rate_bound, i, private_stream_coded, id;
94 PutBitContext pb;
95
96 init_put_bits(&pb, buf, 128, NULL, NULL);
97
98 put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
99 put_bits(&pb, 16, 0);
100 put_bits(&pb, 1, 1);
101
102 rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */
103 put_bits(&pb, 22, rate_bound);
104 put_bits(&pb, 1, 1); /* marker */
105 put_bits(&pb, 6, s->audio_bound);
106
107 put_bits(&pb, 1, 1); /* variable bitrate */
108 put_bits(&pb, 1, 1); /* non constrainted bit stream */
109
110 put_bits(&pb, 1, 0); /* audio locked */
111 put_bits(&pb, 1, 0); /* video locked */
112 put_bits(&pb, 1, 1); /* marker */
113
114 put_bits(&pb, 5, s->video_bound);
115 put_bits(&pb, 8, 0xff); /* reserved byte */
116
117 /* audio stream info */
118 private_stream_coded = 0;
119 for(i=0;i<ctx->nb_streams;i++) {
120 StreamInfo *stream = ctx->streams[i]->priv_data;
121 id = stream->id;
122 if (id < 0xc0) {
123 /* special case for private streams (AC3 use that) */
124 if (private_stream_coded)
125 continue;
126 private_stream_coded = 1;
127 id = 0xbd;
128 }
129 put_bits(&pb, 8, id); /* stream ID */
130 put_bits(&pb, 2, 3);
131 if (id < 0xe0) {
132 /* audio */
133 put_bits(&pb, 1, 0);
134 put_bits(&pb, 13, stream->max_buffer_size / 128);
135 } else {
136 /* video */
137 put_bits(&pb, 1, 1);
138 put_bits(&pb, 13, stream->max_buffer_size / 1024);
139 }
140 }
141 flush_put_bits(&pb);
142 size = pbBufPtr(&pb) - pb.buf;
143 /* patch packet size */
144 buf[4] = (size - 6) >> 8;
145 buf[5] = (size - 6) & 0xff;
146
147 return size;
148 }
149
150 static int mpeg_mux_init(AVFormatContext *ctx)
151 {
152 MpegMuxContext *s;
153 int bitrate, i, mpa_id, mpv_id, ac3_id;
154 AVStream *st;
155 StreamInfo *stream;
156
157 s = malloc(sizeof(MpegMuxContext));
158 if (!s)
159 return -1;
160 memset(s, 0, sizeof(MpegMuxContext));
161 ctx->priv_data = s;
162 s->packet_number = 0;
163
164 /* XXX: hardcoded */
165 s->packet_size = 2048;
166 /* startcode(4) + length(2) + flags(1) */
167 s->packet_data_max_size = s->packet_size - 7;
168 s->audio_bound = 0;
169 s->video_bound = 0;
170 mpa_id = AUDIO_ID;
171 ac3_id = 0x80;
172 mpv_id = VIDEO_ID;
173 for(i=0;i<ctx->nb_streams;i++) {
174 st = ctx->streams[i];
175 stream = av_mallocz(sizeof(StreamInfo));
176 if (!stream)
177 goto fail;
178 st->priv_data = stream;
179
180 switch(st->codec.codec_type) {
181 case CODEC_TYPE_AUDIO:
182 if (st->codec.codec_id == CODEC_ID_AC3)
183 stream->id = ac3_id++;
184 else
185 stream->id = mpa_id++;
186 stream->max_buffer_size = 4 * 1024;
187 s->audio_bound++;
188 break;
189 case CODEC_TYPE_VIDEO:
190 stream->id = mpv_id++;
191 stream->max_buffer_size = 46 * 1024;
192 s->video_bound++;
193 break;
194 }
195 }
196
197 /* we increase slightly the bitrate to take into account the
198 headers. XXX: compute it exactly */
199 bitrate = 2000;
200 for(i=0;i<ctx->nb_streams;i++) {
201 st = ctx->streams[i];
202 bitrate += st->codec.bit_rate;
203 }
204 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
205 /* every 2 seconds */
206 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
207 /* every 10 seconds */
208 s->system_header_freq = s->pack_header_freq * 5;
209
210 for(i=0;i<ctx->nb_streams;i++) {
211 stream = ctx->streams[i]->priv_data;
212 stream->buffer_ptr = 0;
213 stream->packet_number = 0;
214 stream->pts = 0;
215 stream->start_pts = -1;
216
217 st = ctx->streams[i];
218 switch (st->codec.codec_type) {
219 case CODEC_TYPE_AUDIO:
220 ticker_init(&stream->pts_ticker,
221 st->codec.sample_rate,
222 90000 * st->codec.frame_size);
223 break;
224 case CODEC_TYPE_VIDEO:
225 ticker_init(&stream->pts_ticker,
226 st->codec.frame_rate,
227 90000 * FRAME_RATE_BASE);
228 break;
229 }
230 }
231 return 0;
232 fail:
233 for(i=0;i<ctx->nb_streams;i++) {
234 free(ctx->streams[i]->priv_data);
235 }
236 free(s);
237 return -ENOMEM;
238 }
239
240 /* flush the packet on stream stream_index */
241 static void flush_packet(AVFormatContext *ctx, int stream_index)
242 {
243 MpegMuxContext *s = ctx->priv_data;
244 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
245 UINT8 *buf_ptr;
246 int size, payload_size, startcode, id, len, stuffing_size, i;
247 INT64 timestamp;
248 UINT8 buffer[128];
249
250 id = stream->id;
251 timestamp = stream->start_pts;
252
253 #if 0
254 printf("packet ID=%2x PTS=%0.3f\n",
255 id, timestamp / 90000.0);
256 #endif
257
258 buf_ptr = buffer;
259 if ((s->packet_number % s->pack_header_freq) == 0) {
260 /* output pack and systems header if needed */
261 size = put_pack_header(ctx, buf_ptr, timestamp);
262 buf_ptr += size;
263 if ((s->packet_number % s->system_header_freq) == 0) {
264 size = put_system_header(ctx, buf_ptr);
265 buf_ptr += size;
266 }
267 }
268 size = buf_ptr - buffer;
269 put_buffer(&ctx->pb, buffer, size);
270
271 /* packet header */
272 payload_size = s->packet_size - (size + 6 + 5);
273 if (id < 0xc0) {
274 startcode = PRIVATE_STREAM_1;
275 payload_size -= 4;
276 } else {
277 startcode = 0x100 + id;
278 }
279 stuffing_size = payload_size - stream->buffer_ptr;
280 if (stuffing_size < 0)
281 stuffing_size = 0;
282
283 put_be32(&ctx->pb, startcode);
284
285 put_be16(&ctx->pb, payload_size + 5);
286 /* stuffing */
287 for(i=0;i<stuffing_size;i++)
288 put_byte(&ctx->pb, 0xff);
289
290 /* presentation time stamp */
291 put_byte(&ctx->pb,
292 (0x02 << 4) |
293 (((timestamp >> 30) & 0x07) << 1) |
294 1);
295 put_be16(&ctx->pb, (UINT16)((((timestamp >> 15) & 0x7fff) << 1) | 1));
296 put_be16(&ctx->pb, (UINT16)((((timestamp) & 0x7fff) << 1) | 1));
297
298 if (startcode == PRIVATE_STREAM_1) {
299 put_byte(&ctx->pb, id);
300 if (id >= 0x80 && id <= 0xbf) {
301 /* XXX: need to check AC3 spec */
302 put_byte(&ctx->pb, 1);
303 put_byte(&ctx->pb, 0);
304 put_byte(&ctx->pb, 2);
305 }
306 }
307
308 /* output data */
309 put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size);
310 put_flush_packet(&ctx->pb);
311
312 /* preserve remaining data */
313 len = stream->buffer_ptr - payload_size;
314 if (len < 0)
315 len = 0;
316 memmove(stream->buffer, stream->buffer + stream->buffer_ptr - len, len);
317 stream->buffer_ptr = len;
318
319 s->packet_number++;
320 stream->packet_number++;
321 stream->start_pts = -1;
322 }
323
324 static int mpeg_mux_write_packet(AVFormatContext *ctx,
325 int stream_index, UINT8 *buf, int size)
326 {
327 MpegMuxContext *s = ctx->priv_data;
328 AVStream *st = ctx->streams[stream_index];
329 StreamInfo *stream = st->priv_data;
330 int len;
331
332 while (size > 0) {
333 /* set pts */
334 if (stream->start_pts == -1)
335 stream->start_pts = stream->pts;
336 len = s->packet_data_max_size - stream->buffer_ptr;
337 if (len > size)
338 len = size;
339 memcpy(stream->buffer + stream->buffer_ptr, buf, len);
340 stream->buffer_ptr += len;
341 buf += len;
342 size -= len;
343 while (stream->buffer_ptr >= s->packet_data_max_size) {
344 /* output the packet */
345 if (stream->start_pts == -1)
346 stream->start_pts = stream->pts;
347 flush_packet(ctx, stream_index);
348 }
349 }
350
351 stream->pts += ticker_tick(&stream->pts_ticker, 1);
352 return 0;
353 }
354
355 static int mpeg_mux_end(AVFormatContext *ctx)
356 {
357 StreamInfo *stream;
358 int i;
359
360 /* flush each packet */
361 for(i=0;i<ctx->nb_streams;i++) {
362 stream = ctx->streams[i]->priv_data;
363 if (stream->buffer_ptr > 0)
364 flush_packet(ctx, i);
365 }
366
367 /* write the end header */
368 put_be32(&ctx->pb, ISO_11172_END_CODE);
369 put_flush_packet(&ctx->pb);
370 return 0;
371 }
372
373 /*********************************************/
374 /* demux code */
375
376 #define MAX_SYNC_SIZE 100000
377
378 typedef struct MpegDemuxContext {
379 int header_state;
380 int mux_rate; /* 50 byte/s unit */
381 } MpegDemuxContext;
382
383 static int find_start_code(ByteIOContext *pb, int *size_ptr,
384 UINT32 *header_state)
385 {
386 unsigned int state, v;
387 int val, n;
388
389 state = *header_state;
390 n = *size_ptr;
391 while (n > 0) {
392 if (url_feof(pb))
393 break;
394 v = get_byte(pb);
395 n--;
396 if (state == 0x000001) {
397 state = ((state << 8) | v) & 0xffffff;
398 val = state;
399 goto found;
400 }
401 state = ((state << 8) | v) & 0xffffff;
402 }
403 val = -1;
404 found:
405 *header_state = state;
406 *size_ptr = n;
407 return val;
408 }
409
410 static int check_stream_id(AVFormatContext *s, int c_id)
411 {
412 AVStream *st;
413 int i;
414
415 for(i = 0;i < s->nb_streams;i++) {
416 st = s->streams[i];
417 if (st && st->id == c_id)
418 return 1;
419 }
420 return 0;
421 }
422
423 static int mpeg_mux_read_header(AVFormatContext *s,
424 AVFormatParameters *ap)
425 {
426 MpegDemuxContext *m;
427 int size, startcode, c, rate_bound, audio_bound, video_bound, mux_rate, val;
428 int codec_id, n, i, type;
429 AVStream *st;
430 offset_t start_pos;
431
432 m = av_mallocz(sizeof(MpegDemuxContext));
433 if (!m)
434 return -ENOMEM;
435 s->priv_data = m;
436
437 /* search first pack header */
438 m->header_state = 0xff;
439 size = MAX_SYNC_SIZE;
440 start_pos = url_ftell(&s->pb); /* remember this pos */
441 for(;;) {
442 while (size > 0) {
443 startcode = find_start_code(&s->pb, &size, &m->header_state);
444 if (startcode == PACK_START_CODE)
445 goto found;
446 }
447 /* System Header not found find streams searching through file */
448 fprintf(stderr,"libav: MPEG-PS System Header not found!\n");
449 url_fseek(&s->pb, start_pos, SEEK_SET);
450 video_bound = 0;
451 audio_bound = 0;
452 c = 0;
453 s->nb_streams = 0;
454 size = 15*MAX_SYNC_SIZE;
455 while (size > 0) {
456 type = 0;
457 codec_id = 0;
458 n = 0;
459 startcode = find_start_code(&s->pb, &size, &m->header_state);
460 //fprintf(stderr,"\nstartcode: %x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
461 if (startcode == 0x1bd) {
462 url_fseek(&s->pb, -4, SEEK_CUR);
463 size += 4;
464 startcode = mpeg_mux_check_packet(s, &size);
465 //fprintf(stderr,"\nstartcode: %x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
466 if (startcode >= 0x80 && startcode <= 0x9f && !check_stream_id(s, startcode)) {
467 //fprintf(stderr,"Found AC3 stream ID: 0x%x\n", startcode);
468 type = CODEC_TYPE_AUDIO;
469 codec_id = CODEC_ID_AC3;
470 audio_bound++;
471 n = 1;
472 c = startcode;
473 }
474 } else if (startcode == 0x1e0 && !check_stream_id(s, startcode)) {
475 //fprintf(stderr,"Found MPEGVIDEO stream ID: 0x%x\n", startcode);
476 type = CODEC_TYPE_VIDEO;
477 codec_id = CODEC_ID_MPEG1VIDEO;
478 n = 1;
479 c = startcode;
480 video_bound++;
481 } /*else if (startcode >= 0x1c0 && startcode <= 0x1df && !check_stream_id(s, startcode)) {
482 fprintf(stderr,"Found MPEGAUDIO stream ID: 0x%x\n", startcode);
483 type = CODEC_TYPE_AUDIO;
484 codec_id = CODEC_ID_MP2;
485 n = 1;
486 c = startcode;
487 audio_bound++;
488 } */
489 for(i=0;i<n;i++) {
490 st = av_mallocz(sizeof(AVStream));
491 if (!st)
492 return -ENOMEM;
493 s->streams[s->nb_streams++] = st;
494 st->id = c;
495 st->codec.codec_type = type;
496 st->codec.codec_id = codec_id;
497 }
498 }
499 if (video_bound || audio_bound) {
500 url_fseek(&s->pb, start_pos, SEEK_SET);
501 return 0;
502 } else
503 return -ENODATA;
504 found:
505 /* search system header just after pack header */
506 /* parse pack header */
507 get_byte(&s->pb); /* ts1 */
508 get_be16(&s->pb); /* ts2 */
509 get_be16(&s->pb); /* ts3 */
510
511 mux_rate = get_byte(&s->pb) << 16;
512 mux_rate |= get_byte(&s->pb) << 8;
513 mux_rate |= get_byte(&s->pb);
514 mux_rate &= (1 << 22) - 1;
515 m->mux_rate = mux_rate;
516
517 startcode = find_start_code(&s->pb, &size, &m->header_state);
518 if (startcode == SYSTEM_HEADER_START_CODE)
519 break;
520 }
521 size = get_be16(&s->pb);
522 rate_bound = get_byte(&s->pb) << 16;
523 rate_bound |= get_byte(&s->pb) << 8;
524 rate_bound |= get_byte(&s->pb);
525 rate_bound = (rate_bound >> 1) & ((1 << 22) - 1);
526 audio_bound = get_byte(&s->pb) >> 2;
527 video_bound = get_byte(&s->pb) & 0x1f;
528 get_byte(&s->pb); /* reserved byte */
529 #if 0
530 printf("mux_rate=%d kbit/s\n", (m->mux_rate * 50 * 8) / 1000);
531 printf("rate_bound=%d\n", rate_bound);
532 printf("audio_bound=%d\n", audio_bound);
533 printf("video_bound=%d\n", video_bound);
534 #endif
535 size -= 6;
536 s->nb_streams = 0;
537 while (size > 0) {
538 c = get_byte(&s->pb);
539 size--;
540 if ((c & 0x80) == 0)
541 break;
542 val = get_be16(&s->pb);
543 size -= 2;
544 if (c >= 0xc0 && c <= 0xdf) {
545 /* mpeg audio stream */
546 type = CODEC_TYPE_AUDIO;
547 codec_id = CODEC_ID_MP2;
548 n = 1;
549 c = c | 0x100;
550 } else if (c >= 0xe0 && c <= 0xef) {
551 type = CODEC_TYPE_VIDEO;
552 codec_id = CODEC_ID_MPEG1VIDEO;
553 n = 1;
554 c = c | 0x100;
555 } else if (c == 0xb8) {
556 /* all audio streams */
557 /* XXX: hack for DVD: we force AC3, although we do not
558 know that this codec will be used */
559 type = CODEC_TYPE_AUDIO;
560 codec_id = CODEC_ID_AC3;
561 /* XXX: Another hack for DVD: it seems, that AC3 streams
562 aren't signaled on audio_bound on some DVDs (Matrix) */
563 if (audio_bound == 0)
564 audio_bound++;
565 n = audio_bound;
566 c = 0x80;
567 //c = 0x1c0;
568 } else if (c == 0xb9) {
569 /* all video streams */
570 type = CODEC_TYPE_VIDEO;
571 codec_id = CODEC_ID_MPEG1VIDEO;
572 n = video_bound;
573 c = 0x1e0;
574 } else {
575 type = 0;
576 codec_id = 0;
577 n = 0;
578 }
579 for(i=0;i<n;i++) {
580 st = av_mallocz(sizeof(AVStream));
581 if (!st)
582 return -ENOMEM;
583 s->streams[s->nb_streams++] = st;
584 st->id = c + i;
585 st->codec.codec_type = type;
586 st->codec.codec_id = codec_id;
587 }
588 }
589
590 return 0;
591 }
592
593 static INT64 get_pts(ByteIOContext *pb, int c)
594 {
595 INT64 pts;
596 int val;
597
598 if (c < 0)
599 c = get_byte(pb);
600 pts = (INT64)((c >> 1) & 0x07) << 30;
601 val = get_be16(pb);
602 pts |= (INT64)(val >> 1) << 15;
603 val = get_be16(pb);
604 pts |= (INT64)(val >> 1);
605 return pts;
606 }
607
608 static int mpeg_mux_read_packet(AVFormatContext *s,
609 AVPacket *pkt)
610 {
611 MpegDemuxContext *m = s->priv_data;
612 AVStream *st;
613 int len, size, startcode, i, c, flags, header_len;
614 INT64 pts, dts;
615
616 /* next start code (should be immediately after */
617 redo:
618 m->header_state = 0xff;
619 size = MAX_SYNC_SIZE;
620 startcode = find_start_code(&s->pb, &size, &m->header_state);
621 //printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
622 if (startcode < 0)
623 return -EIO;
624 if (startcode == PACK_START_CODE)
625 goto redo;
626 if (startcode == SYSTEM_HEADER_START_CODE)
627 goto redo;
628 if (startcode == PADDING_STREAM ||
629 startcode == PRIVATE_STREAM_2) {
630 /* skip them */
631 len = get_be16(&s->pb);
632 url_fskip(&s->pb, len);
633 goto redo;
634 }
635 /* find matching stream */
636 if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
637 (startcode >= 0x1e0 && startcode <= 0x1ef) ||
638 (startcode == 0x1bd)))
639 goto redo;
640
641 len = get_be16(&s->pb);
642 pts = 0;
643 dts = 0;
644 /* stuffing */
645 for(;;) {
646 c = get_byte(&s->pb);
647 len--;
648 /* XXX: for mpeg1, should test only bit 7 */
649 if (c != 0xff)
650 break;
651 }
652 if ((c & 0xc0) == 0x40) {
653 /* buffer scale & size */
654 get_byte(&s->pb);
655 c = get_byte(&s->pb);
656 len -= 2;
657 }
658 if ((c & 0xf0) == 0x20) {
659 pts = get_pts(&s->pb, c);
660 len -= 4;
661 dts = pts;
662 } else if ((c & 0xf0) == 0x30) {
663 pts = get_pts(&s->pb, c);
664 dts = get_pts(&s->pb, -1);
665 len -= 9;
666 } else if ((c & 0xc0) == 0x80) {
667 /* mpeg 2 PES */
668 if ((c & 0x30) != 0) {
669 fprintf(stderr, "Encrypted multiplex not handled\n");
670 return -EIO;
671 }
672 flags = get_byte(&s->pb);
673 header_len = get_byte(&s->pb);
674 len -= 2;
675 if (header_len > len)
676 goto redo;
677 if ((flags & 0xc0) == 0x40) {
678 pts = get_pts(&s->pb, -1);
679 dts = pts;
680 header_len -= 5;
681 len -= 5;
682 } if ((flags & 0xc0) == 0xc0) {
683 pts = get_pts(&s->pb, -1);
684 dts = get_pts(&s->pb, -1);
685 header_len -= 10;
686 len -= 10;
687 }
688 len -= header_len;
689 while (header_len > 0) {
690 get_byte(&s->pb);
691 header_len--;
692 }
693 }
694 if (startcode == 0x1bd) {
695 startcode = get_byte(&s->pb);
696 len--;
697 if (startcode >= 0x80 && startcode <= 0xbf) {
698 /* audio: skip header */
699 get_byte(&s->pb);
700 get_byte(&s->pb);
701 get_byte(&s->pb);
702 len -= 3;
703 }
704 }
705
706 /* now find stream */
707 for(i=0;i<s->nb_streams;i++) {
708 st = s->streams[i];
709 if (st->id == startcode)
710 goto found;
711 }
712 /* skip packet */
713 url_fskip(&s->pb, len);
714 goto redo;
715 found:
716 av_new_packet(pkt, len);
717 get_buffer(&s->pb, pkt->data, pkt->size);
718 pkt->pts = pts;
719 pkt->stream_index = i;
720 return 0;
721 }
722
723 static int mpeg_mux_check_packet(AVFormatContext *s, int *size)
724 {
725 MpegDemuxContext *m = s->priv_data;
726 int len, startcode, c, n, flags, header_len;
727 INT64 pts, dts;
728
729 /* next start code (should be immediately after */
730 redo:
731 m->header_state = 0xff;
732 startcode = find_start_code(&s->pb, size, &m->header_state);
733
734 if (startcode < 0)
735 return -EIO;
736 if (startcode == PACK_START_CODE)
737 goto redo;
738 if (startcode == SYSTEM_HEADER_START_CODE)
739 goto redo;
740 if (startcode == PADDING_STREAM ||
741 startcode == PRIVATE_STREAM_2) {
742 /* skip them */
743 len = get_be16(&s->pb);
744 url_fskip(&s->pb, len);
745 goto redo;
746 }
747 /* find matching stream */
748 if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
749 (startcode >= 0x1e0 && startcode <= 0x1ef) ||
750 (startcode == 0x1bd)))
751 goto redo;
752
753 n = *size;
754 len = get_be16(&s->pb);
755 n -= 2;
756 pts = 0;
757 dts = 0;
758 /* stuffing */
759 for(;;) {
760 c = get_byte(&s->pb);
761 len--;
762 n--;
763 /* XXX: for mpeg1, should test only bit 7 */
764 if (c != 0xff)
765 break;
766 }
767 if ((c & 0xc0) == 0x40) {
768 /* buffer scale & size */
769 get_byte(&s->pb);
770 c = get_byte(&s->pb);
771 len -= 2;
772 n -= 2;
773 }
774 if ((c & 0xf0) == 0x20) {
775 pts = get_pts(&s->pb, c);
776 len -= 4;
777 n -= 4;
778 dts = pts;
779 } else if ((c & 0xf0) == 0x30) {
780 pts = get_pts(&s->pb, c);
781 dts = get_pts(&s->pb, -1);
782 len -= 9;
783 n -= 9;
784 } else if ((c & 0xc0) == 0x80) {
785 /* mpeg 2 PES */
786 if ((c & 0x30) != 0) {
787 fprintf(stderr, "Encrypted multiplex not handled\n");
788 return -EIO;
789 }
790 flags = get_byte(&s->pb);
791 header_len = get_byte(&s->pb);
792 len -= 2;
793 n -= 2;
794 if (header_len > len)
795 goto redo;
796 if ((flags & 0xc0) == 0x40) {
797 pts = get_pts(&s->pb, -1);
798 dts = pts;
799 header_len -= 5;
800 len -= 5;
801 n -= 5;
802 } if ((flags & 0xc0) == 0xc0) {
803 pts = get_pts(&s->pb, -1);
804 dts = get_pts(&s->pb, -1);
805 header_len -= 10;
806 len -= 10;
807 n -= 10;
808 }
809 len -= header_len;
810 n -= header_len;
811 while (header_len > 0) {
812 get_byte(&s->pb);
813 header_len--;
814 }
815 }
816 if (startcode == 0x1bd) {
817 startcode = get_byte(&s->pb);
818 len--;
819 n--;
820 if (startcode >= 0x80 && startcode <= 0xbf) {
821 /* audio: skip header */
822 get_byte(&s->pb);
823 get_byte(&s->pb);
824 get_byte(&s->pb);
825 len -= 3;
826 n -= 3;
827 }
828 }
829 *size = n;
830 return startcode;
831 }
832
833
834 static int mpeg_mux_read_close(AVFormatContext *s)
835 {
836 MpegDemuxContext *m = s->priv_data;
837 free(m);
838 return 0;
839 }
840
841 AVFormat mpeg_mux_format = {
842 "mpeg",
843 "MPEG multiplex format",
844 "video/x-mpeg",
845 "mpg,mpeg,vob",
846 CODEC_ID_MP2,
847 CODEC_ID_MPEG1VIDEO,
848 mpeg_mux_init,
849 mpeg_mux_write_packet,
850 mpeg_mux_end,
851
852 mpeg_mux_read_header,
853 mpeg_mux_read_packet,
854 mpeg_mux_read_close,
855 };