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.
28 #define MAX_PAYLOAD_SIZE 4096
32 UINT8 buffer
[MAX_PAYLOAD_SIZE
];
35 int max_buffer_size
; /* in bytes */
42 int packet_size
; /* required packet size */
43 int packet_data_max_size
; /* maximum data size inside a packet */
45 int pack_header_freq
; /* frequency (in packets^-1) at which we send pack headers */
46 int system_header_freq
;
47 int mux_rate
; /* bitrate in units of 50 bytes/s */
53 #define PACK_START_CODE ((unsigned int)0x000001ba)
54 #define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb)
55 #define PACKET_START_CODE_MASK ((unsigned int)0xffffff00)
56 #define PACKET_START_CODE_PREFIX ((unsigned int)0x00000100)
57 #define ISO_11172_END_CODE ((unsigned int)0x000001b9)
60 #define PROGRAM_STREAM_MAP 0x1bc
61 #define PRIVATE_STREAM_1 0x1bd
62 #define PADDING_STREAM 0x1be
63 #define PRIVATE_STREAM_2 0x1bf
69 static int put_pack_header(AVFormatContext
*ctx
,
70 UINT8
*buf
, long long timestamp
)
72 MpegMuxContext
*s
= ctx
->priv_data
;
75 init_put_bits(&pb
, buf
, 128, NULL
, NULL
);
77 put_bits(&pb
, 32, PACK_START_CODE
);
78 put_bits(&pb
, 4, 0x2);
79 put_bits(&pb
, 3, (timestamp
>> 30) & 0x07);
81 put_bits(&pb
, 15, (timestamp
>> 15) & 0x7fff);
83 put_bits(&pb
, 15, (timestamp
) & 0x7fff);
86 put_bits(&pb
, 22, s
->mux_rate
);
90 return pb
.buf_ptr
- pb
.buf
;
93 static int put_system_header(AVFormatContext
*ctx
, UINT8
*buf
)
95 MpegMuxContext
*s
= ctx
->priv_data
;
96 int size
, rate_bound
, i
, private_stream_coded
, id
;
99 init_put_bits(&pb
, buf
, 128, NULL
, NULL
);
101 put_bits(&pb
, 32, SYSTEM_HEADER_START_CODE
);
102 put_bits(&pb
, 16, 0);
105 rate_bound
= s
->mux_rate
; /* maximum bit rate of the multiplexed stream */
106 put_bits(&pb
, 22, rate_bound
);
107 put_bits(&pb
, 1, 1); /* marker */
108 put_bits(&pb
, 6, s
->audio_bound
);
110 put_bits(&pb
, 1, 1); /* variable bitrate */
111 put_bits(&pb
, 1, 1); /* non constrainted bit stream */
113 put_bits(&pb
, 1, 0); /* audio locked */
114 put_bits(&pb
, 1, 0); /* video locked */
115 put_bits(&pb
, 1, 1); /* marker */
117 put_bits(&pb
, 5, s
->video_bound
);
118 put_bits(&pb
, 8, 0xff); /* reserved byte */
120 /* audio stream info */
121 private_stream_coded
= 0;
122 for(i
=0;i
<ctx
->nb_streams
;i
++) {
123 StreamInfo
*stream
= ctx
->streams
[i
]->priv_data
;
126 /* special case for private streams (AC3 use that) */
127 if (private_stream_coded
)
129 private_stream_coded
= 1;
132 put_bits(&pb
, 8, id
); /* stream ID */
137 put_bits(&pb
, 13, stream
->max_buffer_size
/ 128);
141 put_bits(&pb
, 13, stream
->max_buffer_size
/ 1024);
145 size
= pb
.buf_ptr
- pb
.buf
;
146 /* patch packet size */
147 buf
[4] = (size
- 6) >> 8;
148 buf
[5] = (size
- 6) & 0xff;
153 static int mpeg_mux_init(AVFormatContext
*ctx
)
156 int bitrate
, i
, mpa_id
, mpv_id
, ac3_id
;
160 s
= malloc(sizeof(MpegMuxContext
));
163 memset(s
, 0, sizeof(MpegMuxContext
));
165 s
->packet_number
= 0;
168 s
->packet_size
= 2048;
169 /* startcode(4) + length(2) + flags(1) */
170 s
->packet_data_max_size
= s
->packet_size
- 7;
176 for(i
=0;i
<ctx
->nb_streams
;i
++) {
177 st
= ctx
->streams
[i
];
178 stream
= av_mallocz(sizeof(StreamInfo
));
181 st
->priv_data
= stream
;
183 switch(st
->codec
.codec_type
) {
184 case CODEC_TYPE_AUDIO
:
185 if (st
->codec
.codec_id
== CODEC_ID_AC3
)
186 stream
->id
= ac3_id
++;
188 stream
->id
= mpa_id
++;
189 stream
->max_buffer_size
= 4 * 1024;
192 case CODEC_TYPE_VIDEO
:
193 stream
->id
= mpv_id
++;
194 stream
->max_buffer_size
= 46 * 1024;
200 /* we increase slightly the bitrate to take into account the
201 headers. XXX: compute it exactly */
203 for(i
=0;i
<ctx
->nb_streams
;i
++) {
204 st
= ctx
->streams
[i
];
205 bitrate
+= st
->codec
.bit_rate
;
207 s
->mux_rate
= (bitrate
+ (8 * 50) - 1) / (8 * 50);
208 /* every 2 seconds */
209 s
->pack_header_freq
= 2 * bitrate
/ s
->packet_size
/ 8;
210 /* every 10 seconds */
211 s
->system_header_freq
= s
->pack_header_freq
* 5;
213 for(i
=0;i
<ctx
->nb_streams
;i
++) {
214 stream
= ctx
->streams
[i
]->priv_data
;
215 stream
->buffer_ptr
= 0;
216 stream
->packet_number
= 0;
218 stream
->start_pts
= -1;
222 for(i
=0;i
<ctx
->nb_streams
;i
++) {
223 free(ctx
->streams
[i
]->priv_data
);
229 /* flush the packet on stream stream_index */
230 static void flush_packet(AVFormatContext
*ctx
, int stream_index
)
232 MpegMuxContext
*s
= ctx
->priv_data
;
233 StreamInfo
*stream
= ctx
->streams
[stream_index
]->priv_data
;
235 int size
, payload_size
, startcode
, id
, len
, stuffing_size
, i
;
240 timestamp
= stream
->start_pts
;
243 printf("packet ID=%2x PTS=%0.3f\n",
244 id
, timestamp
/ 90000.0);
248 if ((s
->packet_number
% s
->pack_header_freq
) == 0) {
249 /* output pack and systems header if needed */
250 size
= put_pack_header(ctx
, buf_ptr
, timestamp
);
252 if ((s
->packet_number
% s
->system_header_freq
) == 0) {
253 size
= put_system_header(ctx
, buf_ptr
);
257 size
= buf_ptr
- buffer
;
258 put_buffer(&ctx
->pb
, buffer
, size
);
261 payload_size
= s
->packet_size
- (size
+ 6 + 5);
263 startcode
= PRIVATE_STREAM_1
;
266 startcode
= 0x100 + id
;
268 stuffing_size
= payload_size
- stream
->buffer_ptr
;
269 if (stuffing_size
< 0)
272 put_be32(&ctx
->pb
, startcode
);
274 put_be16(&ctx
->pb
, payload_size
+ 5);
276 for(i
=0;i
<stuffing_size
;i
++)
277 put_byte(&ctx
->pb
, 0xff);
279 /* presentation time stamp */
282 (((timestamp
>> 30) & 0x07) << 1) |
284 put_be16(&ctx
->pb
, (((timestamp
>> 15) & 0x7fff) << 1) | 1);
285 put_be16(&ctx
->pb
, (((timestamp
) & 0x7fff) << 1) | 1);
287 if (startcode
== PRIVATE_STREAM_1
) {
288 put_byte(&ctx
->pb
, id
);
289 if (id
>= 0x80 && id
<= 0xbf) {
290 /* XXX: need to check AC3 spec */
291 put_byte(&ctx
->pb
, 1);
292 put_byte(&ctx
->pb
, 0);
293 put_byte(&ctx
->pb
, 2);
298 put_buffer(&ctx
->pb
, stream
->buffer
, payload_size
- stuffing_size
);
299 put_flush_packet(&ctx
->pb
);
301 /* preserve remaining data */
302 len
= stream
->buffer_ptr
- payload_size
;
305 memmove(stream
->buffer
, stream
->buffer
+ stream
->buffer_ptr
- len
, len
);
306 stream
->buffer_ptr
= len
;
309 stream
->packet_number
++;
310 stream
->start_pts
= -1;
313 static int mpeg_mux_write_packet(AVFormatContext
*ctx
,
314 int stream_index
, UINT8
*buf
, int size
)
316 MpegMuxContext
*s
= ctx
->priv_data
;
317 AVStream
*st
= ctx
->streams
[stream_index
];
318 StreamInfo
*stream
= st
->priv_data
;
323 if (stream
->start_pts
== -1)
324 stream
->start_pts
= stream
->pts
* 90000.0;
325 len
= s
->packet_data_max_size
- stream
->buffer_ptr
;
328 memcpy(stream
->buffer
+ stream
->buffer_ptr
, buf
, len
);
329 stream
->buffer_ptr
+= len
;
332 while (stream
->buffer_ptr
>= s
->packet_data_max_size
) {
333 /* output the packet */
334 if (stream
->start_pts
== -1)
335 stream
->start_pts
= stream
->pts
* 90000.0;
336 flush_packet(ctx
, stream_index
);
340 if (st
->codec
.codec_type
== CODEC_TYPE_AUDIO
) {
341 stream
->pts
+= (float)st
->codec
.frame_size
/ st
->codec
.sample_rate
;
343 stream
->pts
+= FRAME_RATE_BASE
/ (float)st
->codec
.frame_rate
;
348 static int mpeg_mux_end(AVFormatContext
*ctx
)
353 /* flush each packet */
354 for(i
=0;i
<ctx
->nb_streams
;i
++) {
355 stream
= ctx
->streams
[i
]->priv_data
;
356 if (stream
->buffer_ptr
> 0)
357 flush_packet(ctx
, i
);
360 /* write the end header */
361 put_be32(&ctx
->pb
, ISO_11172_END_CODE
);
362 put_flush_packet(&ctx
->pb
);
366 /*********************************************/
369 #define MAX_SYNC_SIZE 100000
371 typedef struct MpegDemuxContext
{
373 int mux_rate
; /* 50 byte/s unit */
376 static int find_start_code(ByteIOContext
*pb
, int *size_ptr
,
377 UINT32
*header_state
)
379 unsigned int state
, v
;
382 state
= *header_state
;
389 if (state
== 0x000001) {
390 state
= ((state
<< 8) | v
) & 0xffffff;
394 state
= ((state
<< 8) | v
) & 0xffffff;
398 *header_state
= state
;
403 static int mpeg_mux_read_header(AVFormatContext
*s
,
404 AVFormatParameters
*ap
)
407 int size
, startcode
, c
, rate_bound
, audio_bound
, video_bound
, mux_rate
, val
;
408 int codec_id
, n
, i
, type
;
411 m
= av_mallocz(sizeof(MpegDemuxContext
));
416 /* search first pack header */
417 m
->header_state
= 0xff;
418 size
= MAX_SYNC_SIZE
;
421 startcode
= find_start_code(&s
->pb
, &size
, &m
->header_state
);
422 if (startcode
== PACK_START_CODE
)
427 /* search system header just after pack header */
428 /* parse pack header */
429 get_byte(&s
->pb
); /* ts1 */
430 get_be16(&s
->pb
); /* ts2 */
431 get_be16(&s
->pb
); /* ts3 */
433 mux_rate
= get_byte(&s
->pb
) << 16;
434 mux_rate
|= get_byte(&s
->pb
) << 8;
435 mux_rate
|= get_byte(&s
->pb
);
436 mux_rate
&= (1 << 22) - 1;
437 m
->mux_rate
= mux_rate
;
439 startcode
= find_start_code(&s
->pb
, &size
, &m
->header_state
);
440 if (startcode
== SYSTEM_HEADER_START_CODE
)
443 size
= get_be16(&s
->pb
);
444 rate_bound
= get_byte(&s
->pb
) << 16;
445 rate_bound
|= get_byte(&s
->pb
) << 8;
446 rate_bound
|= get_byte(&s
->pb
);
447 rate_bound
= (rate_bound
>> 1) & ((1 << 22) - 1);
448 audio_bound
= get_byte(&s
->pb
) >> 2;
449 video_bound
= get_byte(&s
->pb
) & 0x1f;
450 get_byte(&s
->pb
); /* reserved byte */
452 printf("mux_rate=%d kbit/s\n", (m
->mux_rate
* 50 * 8) / 1000);
453 printf("rate_bound=%d\n", rate_bound
);
454 printf("audio_bound=%d\n", audio_bound
);
455 printf("video_bound=%d\n", video_bound
);
460 c
= get_byte(&s
->pb
);
464 val
= get_be16(&s
->pb
);
466 if (c
>= 0xc0 && c
<= 0xdf) {
467 /* mpeg audio stream */
468 type
= CODEC_TYPE_AUDIO
;
469 codec_id
= CODEC_ID_MP2
;
472 } else if (c
>= 0xe0 && c
<= 0xef) {
473 type
= CODEC_TYPE_VIDEO
;
474 codec_id
= CODEC_ID_MPEG1VIDEO
;
477 } else if (c
== 0xb8) {
478 /* all audio streams */
479 /* XXX: hack for DVD: we force AC3, although we do not
480 know that this codec will be used */
481 type
= CODEC_TYPE_AUDIO
;
482 codec_id
= CODEC_ID_AC3
;
486 } else if (c
== 0xb9) {
487 /* all video streams */
488 type
= CODEC_TYPE_VIDEO
;
489 codec_id
= CODEC_ID_MPEG1VIDEO
;
499 st
= av_mallocz(sizeof(AVStream
));
502 s
->streams
[s
->nb_streams
++] = st
;
504 st
->codec
.codec_type
= type
;
505 st
->codec
.codec_id
= codec_id
;
512 static INT64
get_pts(ByteIOContext
*pb
, int c
)
519 pts
= (INT64
)((c
>> 1) & 0x07) << 30;
521 pts
|= (INT64
)(val
>> 1) << 15;
523 pts
|= (INT64
)(val
>> 1);
527 static int mpeg_mux_read_packet(AVFormatContext
*s
,
530 MpegDemuxContext
*m
= s
->priv_data
;
532 int len
, size
, startcode
, i
, c
, flags
, header_len
;
535 /* next start code (should be immediately after */
537 m
->header_state
= 0xff;
538 size
= MAX_SYNC_SIZE
;
539 startcode
= find_start_code(&s
->pb
, &size
, &m
->header_state
);
540 // printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
543 if (startcode
== PACK_START_CODE
)
545 if (startcode
== SYSTEM_HEADER_START_CODE
)
547 if (startcode
== PADDING_STREAM
||
548 startcode
== PRIVATE_STREAM_2
) {
550 len
= get_be16(&s
->pb
);
551 url_fskip(&s
->pb
, len
);
554 /* find matching stream */
555 if (!((startcode
>= 0x1c0 && startcode
<= 0x1df) ||
556 (startcode
>= 0x1e0 && startcode
<= 0x1ef) ||
557 (startcode
== 0x1bd)))
560 len
= get_be16(&s
->pb
);
565 c
= get_byte(&s
->pb
);
567 /* XXX: for mpeg1, should test only bit 7 */
571 if ((c
& 0xc0) == 0x40) {
572 /* buffer scale & size */
574 c
= get_byte(&s
->pb
);
577 if ((c
& 0xf0) == 0x20) {
578 pts
= get_pts(&s
->pb
, c
);
581 } else if ((c
& 0xf0) == 0x30) {
582 pts
= get_pts(&s
->pb
, c
);
583 dts
= get_pts(&s
->pb
, -1);
585 } else if ((c
& 0xc0) == 0x80) {
587 if ((c
& 0x30) != 0) {
588 fprintf(stderr
, "Encrypted multiplex not handled\n");
591 flags
= get_byte(&s
->pb
);
592 header_len
= get_byte(&s
->pb
);
594 if (header_len
> len
)
596 if ((flags
& 0xc0) == 0x40) {
597 pts
= get_pts(&s
->pb
, -1);
601 } if ((flags
& 0xc0) == 0xc0) {
602 pts
= get_pts(&s
->pb
, -1);
603 dts
= get_pts(&s
->pb
, -1);
608 while (header_len
> 0) {
613 if (startcode
== 0x1bd) {
614 startcode
= get_byte(&s
->pb
);
616 if (startcode
>= 0x80 && startcode
<= 0xbf) {
617 /* audio: skip header */
625 /* now find stream */
626 for(i
=0;i
<s
->nb_streams
;i
++) {
628 if (st
->id
== startcode
)
632 url_fskip(&s
->pb
, len
);
635 av_new_packet(pkt
, len
);
636 get_buffer(&s
->pb
, pkt
->data
, pkt
->size
);
638 pkt
->stream_index
= i
;
642 static int mpeg_mux_read_close(AVFormatContext
*s
)
644 MpegDemuxContext
*m
= s
->priv_data
;
649 AVFormat mpeg_mux_format
= {
651 "MPEG multiplex format",
657 mpeg_mux_write_packet
,
660 mpeg_mux_read_header
,
661 mpeg_mux_read_packet
,