2 * Output a MPEG1 multiplexed video/audio stream
3 * Copyright (c) 2000 Gerard Lantau.
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.
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.
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.
21 #define MAX_PAYLOAD_SIZE 4096
25 UINT8 buffer[MAX_PAYLOAD_SIZE];
28 int max_buffer_size; /* in bytes */
35 int packet_size; /* required packet size */
36 int packet_data_max_size; /* maximum data size inside a packet */
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 */
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)
53 #define PROGRAM_STREAM_MAP 0x1bc
54 #define PRIVATE_STREAM_1 0x1bd
55 #define PADDING_STREAM 0x1be
56 #define PRIVATE_STREAM_2 0x1bf
62 static int put_pack_header(AVFormatContext *ctx,
63 UINT8 *buf, INT64 timestamp)
65 MpegMuxContext *s = ctx->priv_data;
68 init_put_bits(&pb, buf, 128, NULL, NULL);
70 put_bits(&pb, 32, PACK_START_CODE);
71 put_bits(&pb, 4, 0x2);
72 put_bits(&pb, 3, (UINT32)((timestamp >> 30) & 0x07));
74 put_bits(&pb, 15, (UINT32)((timestamp >> 15) & 0x7fff));
76 put_bits(&pb, 15, (UINT32)((timestamp) & 0x7fff));
79 put_bits(&pb, 22, s->mux_rate);
83 return pbBufPtr(&pb) - pb.buf;
86 static int put_system_header(AVFormatContext *ctx, UINT8 *buf)
88 MpegMuxContext *s = ctx->priv_data;
89 int size, rate_bound, i, private_stream_coded, id;
92 init_put_bits(&pb, buf, 128, NULL, NULL);
94 put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
98 rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */
99 put_bits(&pb, 22, rate_bound);
100 put_bits(&pb, 1, 1); /* marker */
101 put_bits(&pb, 6, s->audio_bound);
103 put_bits(&pb, 1, 1); /* variable bitrate */
104 put_bits(&pb, 1, 1); /* non constrainted bit stream */
106 put_bits(&pb, 1, 0); /* audio locked */
107 put_bits(&pb, 1, 0); /* video locked */
108 put_bits(&pb, 1, 1); /* marker */
110 put_bits(&pb, 5, s->video_bound);
111 put_bits(&pb, 8, 0xff); /* reserved byte */
113 /* audio stream info */
114 private_stream_coded = 0;
115 for(i=0;i<ctx->nb_streams;i++) {
116 StreamInfo *stream = ctx->streams[i]->priv_data;
119 /* special case for private streams (AC3 use that) */
120 if (private_stream_coded)
122 private_stream_coded = 1;
125 put_bits(&pb, 8, id); /* stream ID */
130 put_bits(&pb, 13, stream->max_buffer_size / 128);
134 put_bits(&pb, 13, stream->max_buffer_size / 1024);
138 size = pbBufPtr(&pb) - pb.buf;
139 /* patch packet size */
140 buf[4] = (size - 6) >> 8;
141 buf[5] = (size - 6) & 0xff;
146 static int mpeg_mux_init(AVFormatContext *ctx)
149 int bitrate, i, mpa_id, mpv_id, ac3_id;
153 s = malloc(sizeof(MpegMuxContext));
156 memset(s, 0, sizeof(MpegMuxContext));
158 s->packet_number = 0;
161 s->packet_size = 2048;
162 /* startcode(4) + length(2) + flags(1) */
163 s->packet_data_max_size = s->packet_size - 7;
169 for(i=0;i<ctx->nb_streams;i++) {
170 st = ctx->streams[i];
171 stream = av_mallocz(sizeof(StreamInfo));
174 st->priv_data = stream;
176 switch(st->codec.codec_type) {
177 case CODEC_TYPE_AUDIO:
178 if (st->codec.codec_id == CODEC_ID_AC3)
179 stream->id = ac3_id++;
181 stream->id = mpa_id++;
182 stream->max_buffer_size = 4 * 1024;
185 case CODEC_TYPE_VIDEO:
186 stream->id = mpv_id++;
187 stream->max_buffer_size = 46 * 1024;
193 /* we increase slightly the bitrate to take into account the
194 headers. XXX: compute it exactly */
196 for(i=0;i<ctx->nb_streams;i++) {
197 st = ctx->streams[i];
198 bitrate += st->codec.bit_rate;
200 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
201 /* every 2 seconds */
202 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
203 /* every 10 seconds */
204 s->system_header_freq = s->pack_header_freq * 5;
206 for(i=0;i<ctx->nb_streams;i++) {
207 stream = ctx->streams[i]->priv_data;
208 stream->buffer_ptr = 0;
209 stream->packet_number = 0;
211 stream->start_pts = -1;
215 for(i=0;i<ctx->nb_streams;i++) {
216 free(ctx->streams[i]->priv_data);
222 /* flush the packet on stream stream_index */
223 static void flush_packet(AVFormatContext *ctx, int stream_index)
225 MpegMuxContext *s = ctx->priv_data;
226 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
228 int size, payload_size, startcode, id, len, stuffing_size, i;
233 timestamp = stream->start_pts;
236 printf("packet ID=%2x PTS=%0.3f\n",
237 id, timestamp / 90000.0);
241 if ((s->packet_number % s->pack_header_freq) == 0) {
242 /* output pack and systems header if needed */
243 size = put_pack_header(ctx, buf_ptr, timestamp);
245 if ((s->packet_number % s->system_header_freq) == 0) {
246 size = put_system_header(ctx, buf_ptr);
250 size = buf_ptr - buffer;
251 put_buffer(&ctx->pb, buffer, size);
254 payload_size = s->packet_size - (size + 6 + 5);
256 startcode = PRIVATE_STREAM_1;
259 startcode = 0x100 + id;
261 stuffing_size = payload_size - stream->buffer_ptr;
262 if (stuffing_size < 0)
265 put_be32(&ctx->pb, startcode);
267 put_be16(&ctx->pb, payload_size + 5);
269 for(i=0;i<stuffing_size;i++)
270 put_byte(&ctx->pb, 0xff);
272 /* presentation time stamp */
275 (((timestamp >> 30) & 0x07) << 1) |
277 put_be16(&ctx->pb, (UINT16)((((timestamp >> 15) & 0x7fff) << 1) | 1));
278 put_be16(&ctx->pb, (UINT16)((((timestamp) & 0x7fff) << 1) | 1));
280 if (startcode == PRIVATE_STREAM_1) {
281 put_byte(&ctx->pb, id);
282 if (id >= 0x80 && id <= 0xbf) {
283 /* XXX: need to check AC3 spec */
284 put_byte(&ctx->pb, 1);
285 put_byte(&ctx->pb, 0);
286 put_byte(&ctx->pb, 2);
291 put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size);
292 put_flush_packet(&ctx->pb);
294 /* preserve remaining data */
295 len = stream->buffer_ptr - payload_size;
298 memmove(stream->buffer, stream->buffer + stream->buffer_ptr - len, len);
299 stream->buffer_ptr = len;
302 stream->packet_number++;
303 stream->start_pts = -1;
306 static int mpeg_mux_write_packet(AVFormatContext *ctx,
307 int stream_index, UINT8 *buf, int size)
309 MpegMuxContext *s = ctx->priv_data;
310 AVStream *st = ctx->streams[stream_index];
311 StreamInfo *stream = st->priv_data;
316 if (stream->start_pts == -1)
317 stream->start_pts = stream->pts * 90000.0;
318 len = s->packet_data_max_size - stream->buffer_ptr;
321 memcpy(stream->buffer + stream->buffer_ptr, buf, len);
322 stream->buffer_ptr += len;
325 while (stream->buffer_ptr >= s->packet_data_max_size) {
326 /* output the packet */
327 if (stream->start_pts == -1)
328 stream->start_pts = stream->pts * 90000.0;
329 flush_packet(ctx, stream_index);
333 if (st->codec.codec_type == CODEC_TYPE_AUDIO) {
334 stream->pts += (float)st->codec.frame_size / st->codec.sample_rate;
336 stream->pts += FRAME_RATE_BASE / (float)st->codec.frame_rate;
341 static int mpeg_mux_end(AVFormatContext *ctx)
346 /* flush each packet */
347 for(i=0;i<ctx->nb_streams;i++) {
348 stream = ctx->streams[i]->priv_data;
349 if (stream->buffer_ptr > 0)
350 flush_packet(ctx, i);
353 /* write the end header */
354 put_be32(&ctx->pb, ISO_11172_END_CODE);
355 put_flush_packet(&ctx->pb);
359 /*********************************************/
362 #define MAX_SYNC_SIZE 100000
364 typedef struct MpegDemuxContext {
366 int mux_rate; /* 50 byte/s unit */
369 static int find_start_code(ByteIOContext *pb, int *size_ptr,
370 UINT32 *header_state)
372 unsigned int state, v;
375 state = *header_state;
382 if (state == 0x000001) {
383 state = ((state << 8) | v) & 0xffffff;
387 state = ((state << 8) | v) & 0xffffff;
391 *header_state = state;
396 static int mpeg_mux_read_header(AVFormatContext *s,
397 AVFormatParameters *ap)
400 int size, startcode, c, rate_bound, audio_bound, video_bound, mux_rate, val;
401 int codec_id, n, i, type;
404 m = av_mallocz(sizeof(MpegDemuxContext));
409 /* search first pack header */
410 m->header_state = 0xff;
411 size = MAX_SYNC_SIZE;
414 startcode = find_start_code(&s->pb, &size, &m->header_state);
415 if (startcode == PACK_START_CODE)
420 /* search system header just after pack header */
421 /* parse pack header */
422 get_byte(&s->pb); /* ts1 */
423 get_be16(&s->pb); /* ts2 */
424 get_be16(&s->pb); /* ts3 */
426 mux_rate = get_byte(&s->pb) << 16;
427 mux_rate |= get_byte(&s->pb) << 8;
428 mux_rate |= get_byte(&s->pb);
429 mux_rate &= (1 << 22) - 1;
430 m->mux_rate = mux_rate;
432 startcode = find_start_code(&s->pb, &size, &m->header_state);
433 if (startcode == SYSTEM_HEADER_START_CODE)
436 size = get_be16(&s->pb);
437 rate_bound = get_byte(&s->pb) << 16;
438 rate_bound |= get_byte(&s->pb) << 8;
439 rate_bound |= get_byte(&s->pb);
440 rate_bound = (rate_bound >> 1) & ((1 << 22) - 1);
441 audio_bound = get_byte(&s->pb) >> 2;
442 video_bound = get_byte(&s->pb) & 0x1f;
443 get_byte(&s->pb); /* reserved byte */
445 printf("mux_rate=%d kbit/s\n", (m->mux_rate * 50 * 8) / 1000);
446 printf("rate_bound=%d\n", rate_bound);
447 printf("audio_bound=%d\n", audio_bound);
448 printf("video_bound=%d\n", video_bound);
453 c = get_byte(&s->pb);
457 val = get_be16(&s->pb);
459 if (c >= 0xc0 && c <= 0xdf) {
460 /* mpeg audio stream */
461 type = CODEC_TYPE_AUDIO;
462 codec_id = CODEC_ID_MP2;
465 } else if (c >= 0xe0 && c <= 0xef) {
466 type = CODEC_TYPE_VIDEO;
467 codec_id = CODEC_ID_MPEG1VIDEO;
470 } else if (c == 0xb8) {
471 /* all audio streams */
472 /* XXX: hack for DVD: we force AC3, although we do not
473 know that this codec will be used */
474 type = CODEC_TYPE_AUDIO;
475 codec_id = CODEC_ID_AC3;
479 } else if (c == 0xb9) {
480 /* all video streams */
481 type = CODEC_TYPE_VIDEO;
482 codec_id = CODEC_ID_MPEG1VIDEO;
491 st = av_mallocz(sizeof(AVStream));
494 s->streams[s->nb_streams++] = st;
496 st->codec.codec_type = type;
497 st->codec.codec_id = codec_id;
504 static INT64 get_pts(ByteIOContext *pb, int c)
511 pts = (INT64)((c >> 1) & 0x07) << 30;
513 pts |= (INT64)(val >> 1) << 15;
515 pts |= (INT64)(val >> 1);
519 static int mpeg_mux_read_packet(AVFormatContext *s,
522 MpegDemuxContext *m = s->priv_data;
524 int len, size, startcode, i, c, flags, header_len;
527 /* next start code (should be immediately after */
529 m->header_state = 0xff;
530 size = MAX_SYNC_SIZE;
531 startcode = find_start_code(&s->pb, &size, &m->header_state);
532 // printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
535 if (startcode == PACK_START_CODE)
537 if (startcode == SYSTEM_HEADER_START_CODE)
539 if (startcode == PADDING_STREAM ||
540 startcode == PRIVATE_STREAM_2) {
542 len = get_be16(&s->pb);
543 url_fskip(&s->pb, len);
546 /* find matching stream */
547 if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
548 (startcode >= 0x1e0 && startcode <= 0x1ef) ||
549 (startcode == 0x1bd)))
552 len = get_be16(&s->pb);
557 c = get_byte(&s->pb);
559 /* XXX: for mpeg1, should test only bit 7 */
563 if ((c & 0xc0) == 0x40) {
564 /* buffer scale & size */
566 c = get_byte(&s->pb);
569 if ((c & 0xf0) == 0x20) {
570 pts = get_pts(&s->pb, c);
573 } else if ((c & 0xf0) == 0x30) {
574 pts = get_pts(&s->pb, c);
575 dts = get_pts(&s->pb, -1);
577 } else if ((c & 0xc0) == 0x80) {
579 if ((c & 0x30) != 0) {
580 fprintf(stderr, "Encrypted multiplex not handled\n");
583 flags = get_byte(&s->pb);
584 header_len = get_byte(&s->pb);
586 if (header_len > len)
588 if ((flags & 0xc0) == 0x40) {
589 pts = get_pts(&s->pb, -1);
593 } if ((flags & 0xc0) == 0xc0) {
594 pts = get_pts(&s->pb, -1);
595 dts = get_pts(&s->pb, -1);
600 while (header_len > 0) {
605 if (startcode == 0x1bd) {
606 startcode = get_byte(&s->pb);
608 if (startcode >= 0x80 && startcode <= 0xbf) {
609 /* audio: skip header */
617 /* now find stream */
618 for(i=0;i<s->nb_streams;i++) {
620 if (st->id == startcode)
624 url_fskip(&s->pb, len);
627 av_new_packet(pkt, len);
628 get_buffer(&s->pb, pkt->data, pkt->size);
630 pkt->stream_index = i;
634 static int mpeg_mux_read_close(AVFormatContext *s)
636 MpegDemuxContext *m = s->priv_data;
641 AVFormat mpeg_mux_format = {
643 "MPEG multiplex format",
649 mpeg_mux_write_packet,
652 mpeg_mux_read_header,
653 mpeg_mux_read_packet,