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