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