AV synchronisation API
[libav.git] / libav / mpeg.c
1 /*
2 * MPEG1/2 mux/demux
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
4 *
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.
9 *
10 * This library 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 GNU
13 * Lesser General Public License for more details.
14 *
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
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 int is_mpeg2;
47 int is_vcd;
48 } MpegMuxContext;
49
50 #define PACK_START_CODE ((unsigned int)0x000001ba)
51 #define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb)
52 #define SEQUENCE_END_CODE ((unsigned int)0x000001b7)
53 #define PACKET_START_CODE_MASK ((unsigned int)0xffffff00)
54 #define PACKET_START_CODE_PREFIX ((unsigned int)0x00000100)
55 #define ISO_11172_END_CODE ((unsigned int)0x000001b9)
56
57 /* mpeg2 */
58 #define PROGRAM_STREAM_MAP 0x1bc
59 #define PRIVATE_STREAM_1 0x1bd
60 #define PADDING_STREAM 0x1be
61 #define PRIVATE_STREAM_2 0x1bf
62
63
64 #define AUDIO_ID 0xc0
65 #define VIDEO_ID 0xe0
66
67 extern AVOutputFormat mpeg1system_mux;
68 extern AVOutputFormat mpeg1vcd_mux;
69 extern AVOutputFormat mpeg2vob_mux;
70
71 static int put_pack_header(AVFormatContext *ctx,
72 UINT8 *buf, INT64 timestamp)
73 {
74 MpegMuxContext *s = ctx->priv_data;
75 PutBitContext pb;
76
77 init_put_bits(&pb, buf, 128, NULL, NULL);
78
79 put_bits(&pb, 32, PACK_START_CODE);
80 put_bits(&pb, 4, 0x2);
81 put_bits(&pb, 3, (UINT32)((timestamp >> 30) & 0x07));
82 put_bits(&pb, 1, 1);
83 put_bits(&pb, 15, (UINT32)((timestamp >> 15) & 0x7fff));
84 put_bits(&pb, 1, 1);
85 put_bits(&pb, 15, (UINT32)((timestamp) & 0x7fff));
86 put_bits(&pb, 1, 1);
87 put_bits(&pb, 1, 1);
88 put_bits(&pb, 22, s->mux_rate);
89 put_bits(&pb, 1, 1);
90
91 flush_put_bits(&pb);
92 return pbBufPtr(&pb) - pb.buf;
93 }
94
95 static int put_system_header(AVFormatContext *ctx, UINT8 *buf)
96 {
97 MpegMuxContext *s = ctx->priv_data;
98 int size, rate_bound, i, private_stream_coded, id;
99 PutBitContext pb;
100
101 init_put_bits(&pb, buf, 128, NULL, NULL);
102
103 put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
104 put_bits(&pb, 16, 0);
105 put_bits(&pb, 1, 1);
106
107 rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */
108 put_bits(&pb, 22, rate_bound);
109 put_bits(&pb, 1, 1); /* marker */
110 put_bits(&pb, 6, s->audio_bound);
111
112 put_bits(&pb, 1, 1); /* variable bitrate */
113 put_bits(&pb, 1, 1); /* non constrainted bit stream */
114
115 put_bits(&pb, 1, 0); /* audio locked */
116 put_bits(&pb, 1, 0); /* video locked */
117 put_bits(&pb, 1, 1); /* marker */
118
119 put_bits(&pb, 5, s->video_bound);
120 put_bits(&pb, 8, 0xff); /* reserved byte */
121
122 /* audio stream info */
123 private_stream_coded = 0;
124 for(i=0;i<ctx->nb_streams;i++) {
125 StreamInfo *stream = ctx->streams[i]->priv_data;
126 id = stream->id;
127 if (id < 0xc0) {
128 /* special case for private streams (AC3 use that) */
129 if (private_stream_coded)
130 continue;
131 private_stream_coded = 1;
132 id = 0xbd;
133 }
134 put_bits(&pb, 8, id); /* stream ID */
135 put_bits(&pb, 2, 3);
136 if (id < 0xe0) {
137 /* audio */
138 put_bits(&pb, 1, 0);
139 put_bits(&pb, 13, stream->max_buffer_size / 128);
140 } else {
141 /* video */
142 put_bits(&pb, 1, 1);
143 put_bits(&pb, 13, stream->max_buffer_size / 1024);
144 }
145 }
146 flush_put_bits(&pb);
147 size = pbBufPtr(&pb) - pb.buf;
148 /* patch packet size */
149 buf[4] = (size - 6) >> 8;
150 buf[5] = (size - 6) & 0xff;
151
152 return size;
153 }
154
155 static int mpeg_mux_init(AVFormatContext *ctx)
156 {
157 MpegMuxContext *s = ctx->priv_data;
158 int bitrate, i, mpa_id, mpv_id, ac3_id;
159 AVStream *st;
160 StreamInfo *stream;
161
162 s->packet_number = 0;
163 s->is_vcd = (ctx->oformat == &mpeg1vcd_mux);
164 s->is_mpeg2 = (ctx->oformat == &mpeg2vob_mux);
165
166 if (s->is_vcd)
167 s->packet_size = 2324; /* VCD packet size */
168 else
169 s->packet_size = 2048;
170
171 /* startcode(4) + length(2) + flags(1) */
172 s->packet_data_max_size = s->packet_size - 7;
173 s->audio_bound = 0;
174 s->video_bound = 0;
175 mpa_id = AUDIO_ID;
176 ac3_id = 0x80;
177 mpv_id = VIDEO_ID;
178 for(i=0;i<ctx->nb_streams;i++) {
179 st = ctx->streams[i];
180 stream = av_mallocz(sizeof(StreamInfo));
181 if (!stream)
182 goto fail;
183 st->priv_data = stream;
184
185 switch(st->codec.codec_type) {
186 case CODEC_TYPE_AUDIO:
187 if (st->codec.codec_id == CODEC_ID_AC3)
188 stream->id = ac3_id++;
189 else
190 stream->id = mpa_id++;
191 stream->max_buffer_size = 4 * 1024;
192 s->audio_bound++;
193 break;
194 case CODEC_TYPE_VIDEO:
195 stream->id = mpv_id++;
196 stream->max_buffer_size = 46 * 1024;
197 s->video_bound++;
198 break;
199 default:
200 av_abort();
201 }
202 }
203
204 /* we increase slightly the bitrate to take into account the
205 headers. XXX: compute it exactly */
206 bitrate = 2000;
207 for(i=0;i<ctx->nb_streams;i++) {
208 st = ctx->streams[i];
209 bitrate += st->codec.bit_rate;
210 }
211 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
212
213 if (s->is_vcd || s->is_mpeg2)
214 /* every packet */
215 s->pack_header_freq = 1;
216 else
217 /* every 2 seconds */
218 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
219
220 if (s->is_vcd)
221 /* every 40 packets, this is my invention */
222 s->system_header_freq = s->pack_header_freq * 40;
223 else
224 /* every 10 seconds */
225 s->system_header_freq = s->pack_header_freq * 5;
226
227
228 for(i=0;i<ctx->nb_streams;i++) {
229 stream = ctx->streams[i]->priv_data;
230 stream->buffer_ptr = 0;
231 stream->packet_number = 0;
232 stream->pts = 0;
233 stream->start_pts = -1;
234
235 st = ctx->streams[i];
236 switch (st->codec.codec_type) {
237 case CODEC_TYPE_AUDIO:
238 ticker_init(&stream->pts_ticker,
239 st->codec.sample_rate,
240 90000 * st->codec.frame_size);
241 break;
242 case CODEC_TYPE_VIDEO:
243 ticker_init(&stream->pts_ticker,
244 st->codec.frame_rate,
245 90000 * FRAME_RATE_BASE);
246 break;
247 default:
248 av_abort();
249 }
250 }
251 return 0;
252 fail:
253 for(i=0;i<ctx->nb_streams;i++) {
254 av_free(ctx->streams[i]->priv_data);
255 }
256 return -ENOMEM;
257 }
258
259 /* flush the packet on stream stream_index */
260 static void flush_packet(AVFormatContext *ctx, int stream_index, int last_pkt)
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, header_len;
266 INT64 timestamp;
267 UINT8 buffer[128];
268 int last = last_pkt ? 4 : 0;
269
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;
279 if (((s->packet_number % s->pack_header_freq) == 0)) {
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 */
292 if (s->is_mpeg2) {
293 header_len = 8;
294 } else {
295 header_len = 5;
296 }
297 payload_size = s->packet_size - (size + 6 + header_len + last);
298 if (id < 0xc0) {
299 startcode = PRIVATE_STREAM_1;
300 payload_size -= 4;
301 } else {
302 startcode = 0x100 + id;
303 }
304 stuffing_size = payload_size - stream->buffer_ptr;
305 if (stuffing_size < 0)
306 stuffing_size = 0;
307
308 put_be32(&ctx->pb, startcode);
309
310 put_be16(&ctx->pb, payload_size + header_len);
311 /* stuffing */
312 for(i=0;i<stuffing_size;i++)
313 put_byte(&ctx->pb, 0xff);
314
315 if (s->is_mpeg2) {
316 put_byte(&ctx->pb, 0x80); /* mpeg2 id */
317 put_byte(&ctx->pb, 0x80); /* flags */
318 put_byte(&ctx->pb, 0x05); /* header len (only pts is included) */
319 }
320 put_byte(&ctx->pb,
321 (0x02 << 4) |
322 (((timestamp >> 30) & 0x07) << 1) |
323 1);
324 put_be16(&ctx->pb, (UINT16)((((timestamp >> 15) & 0x7fff) << 1) | 1));
325 put_be16(&ctx->pb, (UINT16)((((timestamp) & 0x7fff) << 1) | 1));
326
327 if (startcode == PRIVATE_STREAM_1) {
328 put_byte(&ctx->pb, id);
329 if (id >= 0x80 && id <= 0xbf) {
330 /* XXX: need to check AC3 spec */
331 put_byte(&ctx->pb, 1);
332 put_byte(&ctx->pb, 0);
333 put_byte(&ctx->pb, 2);
334 }
335 }
336
337 if (last_pkt) {
338 put_be32(&ctx->pb, ISO_11172_END_CODE);
339 }
340 /* output data */
341 put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size);
342 put_flush_packet(&ctx->pb);
343
344 /* preserve remaining data */
345 len = stream->buffer_ptr - payload_size;
346 if (len < 0)
347 len = 0;
348 memmove(stream->buffer, stream->buffer + stream->buffer_ptr - len, len);
349 stream->buffer_ptr = len;
350
351 s->packet_number++;
352 stream->packet_number++;
353 stream->start_pts = -1;
354 }
355
356 static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index,
357 UINT8 *buf, int size, int force_pts)
358 {
359 MpegMuxContext *s = ctx->priv_data;
360 AVStream *st = ctx->streams[stream_index];
361 StreamInfo *stream = st->priv_data;
362 int len;
363
364 while (size > 0) {
365 /* set pts */
366 if (stream->start_pts == -1) {
367 if (force_pts)
368 stream->pts = force_pts;
369 stream->start_pts = stream->pts;
370 }
371 len = s->packet_data_max_size - stream->buffer_ptr;
372 if (len > size)
373 len = size;
374 memcpy(stream->buffer + stream->buffer_ptr, buf, len);
375 stream->buffer_ptr += len;
376 buf += len;
377 size -= len;
378 while (stream->buffer_ptr >= s->packet_data_max_size) {
379 /* output the packet */
380 if (stream->start_pts == -1)
381 stream->start_pts = stream->pts;
382 flush_packet(ctx, stream_index, 0);
383 }
384 }
385
386 stream->pts += ticker_tick(&stream->pts_ticker, 1);
387 //if (st->codec.codec_type == CODEC_TYPE_VIDEO)
388 // fprintf(stderr,"\nVideo PTS: %6lld", stream->pts);
389 //else
390 // fprintf(stderr,"\nAudio PTS: %6lld", stream->pts);
391 return 0;
392 }
393
394 static int mpeg_mux_end(AVFormatContext *ctx)
395 {
396 StreamInfo *stream;
397 int i;
398
399 /* flush each packet */
400 for(i=0;i<ctx->nb_streams;i++) {
401 stream = ctx->streams[i]->priv_data;
402 if (stream->buffer_ptr > 0) {
403 if (i == (ctx->nb_streams - 1))
404 flush_packet(ctx, i, 1);
405 else
406 flush_packet(ctx, i, 0);
407 }
408 }
409
410 /* write the end header */
411 //put_be32(&ctx->pb, ISO_11172_END_CODE);
412 //put_flush_packet(&ctx->pb);
413 return 0;
414 }
415
416 /*********************************************/
417 /* demux code */
418
419 #define MAX_SYNC_SIZE 100000
420
421 static int mpegps_probe(AVProbeData *p)
422 {
423 int code, c, i;
424 code = 0xff;
425
426 /* we search the first start code. If it is a packet start code,
427 then we decide it is mpeg ps. We do not send highest value to
428 give a chance to mpegts */
429 for(i=0;i<p->buf_size;i++) {
430 c = p->buf[i];
431 code = (code << 8) | c;
432 if ((code & 0xffffff00) == 0x100) {
433 if (code == PACK_START_CODE ||
434 code == SYSTEM_HEADER_START_CODE ||
435 (code >= 0x1e0 && code <= 0x1ef) ||
436 (code >= 0x1c0 && code <= 0x1df) ||
437 code == PRIVATE_STREAM_2 ||
438 code == PROGRAM_STREAM_MAP ||
439 code == PRIVATE_STREAM_1 ||
440 code == PADDING_STREAM)
441 return AVPROBE_SCORE_MAX - 1;
442 else
443 return 0;
444 }
445 }
446 return 0;
447 }
448
449
450 typedef struct MpegDemuxContext {
451 int header_state;
452 } MpegDemuxContext;
453
454 static int find_start_code(ByteIOContext *pb, int *size_ptr,
455 UINT32 *header_state)
456 {
457 unsigned int state, v;
458 int val, n;
459
460 state = *header_state;
461 n = *size_ptr;
462 while (n > 0) {
463 if (url_feof(pb))
464 break;
465 v = get_byte(pb);
466 n--;
467 if (state == 0x000001) {
468 state = ((state << 8) | v) & 0xffffff;
469 val = state;
470 goto found;
471 }
472 state = ((state << 8) | v) & 0xffffff;
473 }
474 val = -1;
475 found:
476 *header_state = state;
477 *size_ptr = n;
478 return val;
479 }
480
481 static int mpegps_read_header(AVFormatContext *s,
482 AVFormatParameters *ap)
483 {
484 MpegDemuxContext *m = s->priv_data;
485 m->header_state = 0xff;
486 /* no need to do more */
487 return 0;
488 }
489
490 static INT64 get_pts(ByteIOContext *pb, int c)
491 {
492 INT64 pts;
493 int val;
494
495 if (c < 0)
496 c = get_byte(pb);
497 pts = (INT64)((c >> 1) & 0x07) << 30;
498 val = get_be16(pb);
499 pts |= (INT64)(val >> 1) << 15;
500 val = get_be16(pb);
501 pts |= (INT64)(val >> 1);
502 return pts;
503 }
504
505 static int mpegps_read_packet(AVFormatContext *s,
506 AVPacket *pkt)
507 {
508 MpegDemuxContext *m = s->priv_data;
509 AVStream *st;
510 int len, size, startcode, i, c, flags, header_len, type, codec_id;
511 INT64 pts, dts;
512
513 /* next start code (should be immediately after */
514 redo:
515 m->header_state = 0xff;
516 size = MAX_SYNC_SIZE;
517 startcode = find_start_code(&s->pb, &size, &m->header_state);
518 //printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
519 if (startcode < 0)
520 return -EIO;
521 if (startcode == PACK_START_CODE)
522 goto redo;
523 if (startcode == SYSTEM_HEADER_START_CODE)
524 goto redo;
525 if (startcode == PADDING_STREAM ||
526 startcode == PRIVATE_STREAM_2) {
527 /* skip them */
528 len = get_be16(&s->pb);
529 url_fskip(&s->pb, len);
530 goto redo;
531 }
532 /* find matching stream */
533 if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
534 (startcode >= 0x1e0 && startcode <= 0x1ef) ||
535 (startcode == 0x1bd)))
536 goto redo;
537
538 len = get_be16(&s->pb);
539 pts = 0;
540 dts = 0;
541 /* stuffing */
542 for(;;) {
543 c = get_byte(&s->pb);
544 len--;
545 /* XXX: for mpeg1, should test only bit 7 */
546 if (c != 0xff)
547 break;
548 }
549 if ((c & 0xc0) == 0x40) {
550 /* buffer scale & size */
551 get_byte(&s->pb);
552 c = get_byte(&s->pb);
553 len -= 2;
554 }
555 if ((c & 0xf0) == 0x20) {
556 pts = get_pts(&s->pb, c);
557 len -= 4;
558 dts = pts;
559 } else if ((c & 0xf0) == 0x30) {
560 pts = get_pts(&s->pb, c);
561 dts = get_pts(&s->pb, -1);
562 len -= 9;
563 } else if ((c & 0xc0) == 0x80) {
564 /* mpeg 2 PES */
565 if ((c & 0x30) != 0) {
566 fprintf(stderr, "Encrypted multiplex not handled\n");
567 return -EIO;
568 }
569 flags = get_byte(&s->pb);
570 header_len = get_byte(&s->pb);
571 len -= 2;
572 if (header_len > len)
573 goto redo;
574 if ((flags & 0xc0) == 0x80) {
575 pts = get_pts(&s->pb, -1);
576 dts = pts;
577 header_len -= 5;
578 len -= 5;
579 } if ((flags & 0xc0) == 0xc0) {
580 pts = get_pts(&s->pb, -1);
581 dts = get_pts(&s->pb, -1);
582 header_len -= 10;
583 len -= 10;
584 }
585 len -= header_len;
586 while (header_len > 0) {
587 get_byte(&s->pb);
588 header_len--;
589 }
590 }
591 if (startcode == 0x1bd) {
592 startcode = get_byte(&s->pb);
593 len--;
594 if (startcode >= 0x80 && startcode <= 0xbf) {
595 /* audio: skip header */
596 get_byte(&s->pb);
597 get_byte(&s->pb);
598 get_byte(&s->pb);
599 len -= 3;
600 }
601 }
602
603 /* now find stream */
604 for(i=0;i<s->nb_streams;i++) {
605 st = s->streams[i];
606 if (st->id == startcode)
607 goto found;
608 }
609 if (startcode >= 0x1e0 && startcode <= 0x1ef) {
610 type = CODEC_TYPE_VIDEO;
611 codec_id = CODEC_ID_MPEG1VIDEO;
612 } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
613 type = CODEC_TYPE_AUDIO;
614 codec_id = CODEC_ID_MP2;
615 } else if (startcode >= 0x80 && startcode <= 0x9f) {
616 type = CODEC_TYPE_AUDIO;
617 codec_id = CODEC_ID_AC3;
618 } else {
619 skip:
620 /* skip packet */
621 url_fskip(&s->pb, len);
622 goto redo;
623 }
624 /* no stream found: add a new stream */
625 st = av_new_stream(s, startcode);
626 if (!st)
627 goto skip;
628 st->codec.codec_type = type;
629 st->codec.codec_id = codec_id;
630 found:
631 av_new_packet(pkt, len);
632 //printf("\nRead Packet ID: %x PTS: %f Size: %d", startcode,
633 // (float)pts/90000, len);
634 get_buffer(&s->pb, pkt->data, pkt->size);
635 pkt->pts = pts;
636 pkt->stream_index = st->index;
637 return 0;
638 }
639
640 static int mpegps_read_close(AVFormatContext *s)
641 {
642 return 0;
643 }
644
645 static AVOutputFormat mpeg1system_mux = {
646 "mpeg",
647 "MPEG1 System format",
648 "video/x-mpeg",
649 "mpg,mpeg",
650 sizeof(MpegMuxContext),
651 CODEC_ID_MP2,
652 CODEC_ID_MPEG1VIDEO,
653 mpeg_mux_init,
654 mpeg_mux_write_packet,
655 mpeg_mux_end,
656 };
657
658 static AVOutputFormat mpeg1vcd_mux = {
659 "vcd",
660 "MPEG1 System format (VCD)",
661 "video/x-mpeg",
662 NULL,
663 sizeof(MpegMuxContext),
664 CODEC_ID_MP2,
665 CODEC_ID_MPEG1VIDEO,
666 mpeg_mux_init,
667 mpeg_mux_write_packet,
668 mpeg_mux_end,
669 };
670
671 static AVOutputFormat mpeg2vob_mux = {
672 "vob",
673 "MPEG2 PS format (VOB)",
674 "video/x-mpeg",
675 "vob",
676 sizeof(MpegMuxContext),
677 CODEC_ID_MP2,
678 CODEC_ID_MPEG1VIDEO,
679 mpeg_mux_init,
680 mpeg_mux_write_packet,
681 mpeg_mux_end,
682 };
683
684 static AVInputFormat mpegps_demux = {
685 "mpeg",
686 "MPEG PS format",
687 sizeof(MpegDemuxContext),
688 mpegps_probe,
689 mpegps_read_header,
690 mpegps_read_packet,
691 mpegps_read_close,
692 .flags = AVFMT_NOHEADER,
693 };
694
695 int mpegps_init(void)
696 {
697 av_register_output_format(&mpeg1system_mux);
698 av_register_output_format(&mpeg1vcd_mux);
699 av_register_output_format(&mpeg2vob_mux);
700 av_register_input_format(&mpegps_demux);
701 return 0;
702 }