3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
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.
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.
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
22 #define MAX_PAYLOAD_SIZE 4096
26 UINT8 buffer[MAX_PAYLOAD_SIZE];
29 int max_buffer_size; /* in bytes */
37 int packet_size; /* required packet size */
38 int packet_data_max_size; /* maximum data size inside a packet */
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 */
48 #define PACK_START_CODE ((unsigned int)0x000001ba)
49 #define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb)
50 #define SEQUENCE_END_CODE ((unsigned int)0x000001b7)
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)
56 #define PROGRAM_STREAM_MAP 0x1bc
57 #define PRIVATE_STREAM_1 0x1bd
58 #define PADDING_STREAM 0x1be
59 #define PRIVATE_STREAM_2 0x1bf
65 static int put_pack_header(AVFormatContext *ctx,
66 UINT8 *buf, INT64 timestamp)
68 MpegMuxContext *s = ctx->priv_data;
71 init_put_bits(&pb, buf, 128, NULL, NULL);
73 put_bits(&pb, 32, PACK_START_CODE);
74 put_bits(&pb, 4, 0x2);
75 put_bits(&pb, 3, (UINT32)((timestamp >> 30) & 0x07));
77 put_bits(&pb, 15, (UINT32)((timestamp >> 15) & 0x7fff));
79 put_bits(&pb, 15, (UINT32)((timestamp) & 0x7fff));
82 put_bits(&pb, 22, s->mux_rate);
86 return pbBufPtr(&pb) - pb.buf;
89 static int put_system_header(AVFormatContext *ctx, UINT8 *buf)
91 MpegMuxContext *s = ctx->priv_data;
92 int size, rate_bound, i, private_stream_coded, id;
95 init_put_bits(&pb, buf, 128, NULL, NULL);
97 put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
101 rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */
102 put_bits(&pb, 22, rate_bound);
103 put_bits(&pb, 1, 1); /* marker */
104 put_bits(&pb, 6, s->audio_bound);
106 put_bits(&pb, 1, 1); /* variable bitrate */
107 put_bits(&pb, 1, 1); /* non constrainted bit stream */
109 put_bits(&pb, 1, 0); /* audio locked */
110 put_bits(&pb, 1, 0); /* video locked */
111 put_bits(&pb, 1, 1); /* marker */
113 put_bits(&pb, 5, s->video_bound);
114 put_bits(&pb, 8, 0xff); /* reserved byte */
116 /* audio stream info */
117 private_stream_coded = 0;
118 for(i=0;i<ctx->nb_streams;i++) {
119 StreamInfo *stream = ctx->streams[i]->priv_data;
122 /* special case for private streams (AC3 use that) */
123 if (private_stream_coded)
125 private_stream_coded = 1;
128 put_bits(&pb, 8, id); /* stream ID */
133 put_bits(&pb, 13, stream->max_buffer_size / 128);
137 put_bits(&pb, 13, stream->max_buffer_size / 1024);
141 size = pbBufPtr(&pb) - pb.buf;
142 /* patch packet size */
143 buf[4] = (size - 6) >> 8;
144 buf[5] = (size - 6) & 0xff;
149 static int mpeg_mux_init(AVFormatContext *ctx)
151 MpegMuxContext *s = ctx->priv_data;
152 int bitrate, i, mpa_id, mpv_id, ac3_id;
156 s->packet_number = 0;
159 if (ctx->flags & AVF_FLAG_VCD)
160 s->packet_size = 2324; /* VCD packet size */
162 s->packet_size = 2048;
164 /* startcode(4) + length(2) + flags(1) */
165 s->packet_data_max_size = s->packet_size - 7;
171 for(i=0;i<ctx->nb_streams;i++) {
172 st = ctx->streams[i];
173 stream = av_mallocz(sizeof(StreamInfo));
176 st->priv_data = stream;
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++;
183 stream->id = mpa_id++;
184 stream->max_buffer_size = 4 * 1024;
187 case CODEC_TYPE_VIDEO:
188 stream->id = mpv_id++;
189 stream->max_buffer_size = 46 * 1024;
197 /* we increase slightly the bitrate to take into account the
198 headers. XXX: compute it exactly */
200 for(i=0;i<ctx->nb_streams;i++) {
201 st = ctx->streams[i];
202 bitrate += st->codec.bit_rate;
204 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
206 if (ctx->flags & AVF_FLAG_VCD)
208 s->pack_header_freq = 1;
210 /* every 2 seconds */
211 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
213 if (ctx->flags & AVF_FLAG_VCD)
214 /* every 40 packets, this is my invention */
215 s->system_header_freq = s->pack_header_freq * 40;
217 /* every 10 seconds */
218 s->system_header_freq = s->pack_header_freq * 5;
221 for(i=0;i<ctx->nb_streams;i++) {
222 stream = ctx->streams[i]->priv_data;
223 stream->buffer_ptr = 0;
224 stream->packet_number = 0;
226 stream->start_pts = -1;
228 st = ctx->streams[i];
229 switch (st->codec.codec_type) {
230 case CODEC_TYPE_AUDIO:
231 ticker_init(&stream->pts_ticker,
232 st->codec.sample_rate,
233 90000 * st->codec.frame_size);
235 case CODEC_TYPE_VIDEO:
236 ticker_init(&stream->pts_ticker,
237 st->codec.frame_rate,
238 90000 * FRAME_RATE_BASE);
246 for(i=0;i<ctx->nb_streams;i++) {
247 av_free(ctx->streams[i]->priv_data);
252 /* flush the packet on stream stream_index */
253 static void flush_packet(AVFormatContext *ctx, int stream_index, int last_pkt)
255 MpegMuxContext *s = ctx->priv_data;
256 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
258 int size, payload_size, startcode, id, len, stuffing_size, i;
261 int last = last_pkt ? 4 : 0;
264 timestamp = stream->start_pts;
267 printf("packet ID=%2x PTS=%0.3f\n",
268 id, timestamp / 90000.0);
272 if (((s->packet_number % s->pack_header_freq) == 0)) {
273 /* output pack and systems header if needed */
274 size = put_pack_header(ctx, buf_ptr, timestamp);
276 if ((s->packet_number % s->system_header_freq) == 0) {
277 size = put_system_header(ctx, buf_ptr);
281 size = buf_ptr - buffer;
282 put_buffer(&ctx->pb, buffer, size);
285 payload_size = s->packet_size - (size + 6 + 5 + last);
287 startcode = PRIVATE_STREAM_1;
290 startcode = 0x100 + id;
292 stuffing_size = payload_size - stream->buffer_ptr;
293 if (stuffing_size < 0)
296 put_be32(&ctx->pb, startcode);
298 put_be16(&ctx->pb, payload_size + 5);
300 for(i=0;i<stuffing_size;i++)
301 put_byte(&ctx->pb, 0xff);
303 /* presentation time stamp */
306 (((timestamp >> 30) & 0x07) << 1) |
308 put_be16(&ctx->pb, (UINT16)((((timestamp >> 15) & 0x7fff) << 1) | 1));
309 put_be16(&ctx->pb, (UINT16)((((timestamp) & 0x7fff) << 1) | 1));
311 if (startcode == PRIVATE_STREAM_1) {
312 put_byte(&ctx->pb, id);
313 if (id >= 0x80 && id <= 0xbf) {
314 /* XXX: need to check AC3 spec */
315 put_byte(&ctx->pb, 1);
316 put_byte(&ctx->pb, 0);
317 put_byte(&ctx->pb, 2);
322 put_be32(&ctx->pb, ISO_11172_END_CODE);
325 put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size);
326 put_flush_packet(&ctx->pb);
328 /* preserve remaining data */
329 len = stream->buffer_ptr - payload_size;
332 memmove(stream->buffer, stream->buffer + stream->buffer_ptr - len, len);
333 stream->buffer_ptr = len;
336 stream->packet_number++;
337 stream->start_pts = -1;
340 static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index,
341 UINT8 *buf, int size, int force_pts)
343 MpegMuxContext *s = ctx->priv_data;
344 AVStream *st = ctx->streams[stream_index];
345 StreamInfo *stream = st->priv_data;
350 if (stream->start_pts == -1) {
352 stream->pts = force_pts;
353 stream->start_pts = stream->pts;
355 len = s->packet_data_max_size - stream->buffer_ptr;
358 memcpy(stream->buffer + stream->buffer_ptr, buf, len);
359 stream->buffer_ptr += len;
362 while (stream->buffer_ptr >= s->packet_data_max_size) {
363 /* output the packet */
364 if (stream->start_pts == -1)
365 stream->start_pts = stream->pts;
366 flush_packet(ctx, stream_index, 0);
370 stream->pts += ticker_tick(&stream->pts_ticker, 1);
371 //if (st->codec.codec_type == CODEC_TYPE_VIDEO)
372 // fprintf(stderr,"\nVideo PTS: %6lld", stream->pts);
374 // fprintf(stderr,"\nAudio PTS: %6lld", stream->pts);
378 static int mpeg_mux_end(AVFormatContext *ctx)
383 /* flush each packet */
384 for(i=0;i<ctx->nb_streams;i++) {
385 stream = ctx->streams[i]->priv_data;
386 if (stream->buffer_ptr > 0) {
387 if (i == (ctx->nb_streams - 1))
388 flush_packet(ctx, i, 1);
390 flush_packet(ctx, i, 0);
394 /* write the end header */
395 //put_be32(&ctx->pb, ISO_11172_END_CODE);
396 //put_flush_packet(&ctx->pb);
400 /*********************************************/
403 #define MAX_SYNC_SIZE 100000
405 static int mpegps_probe(AVProbeData *p)
410 /* we search the first start code. If it is a packet start code,
411 then we decide it is mpeg ps. We do not send highest value to
412 give a chance to mpegts */
413 for(i=0;i<p->buf_size;i++) {
415 code = (code << 8) | c;
416 if ((code & 0xffffff00) == 0x100) {
417 if (code == PACK_START_CODE ||
418 code == SYSTEM_HEADER_START_CODE ||
419 (code >= 0x1e0 && code <= 0x1ef) ||
420 (code >= 0x1c0 && code <= 0x1df) ||
421 code == PRIVATE_STREAM_2 ||
422 code == PROGRAM_STREAM_MAP ||
423 code == PRIVATE_STREAM_1 ||
424 code == PADDING_STREAM)
425 return AVPROBE_SCORE_MAX - 1;
434 typedef struct MpegDemuxContext {
438 static int find_start_code(ByteIOContext *pb, int *size_ptr,
439 UINT32 *header_state)
441 unsigned int state, v;
444 state = *header_state;
451 if (state == 0x000001) {
452 state = ((state << 8) | v) & 0xffffff;
456 state = ((state << 8) | v) & 0xffffff;
460 *header_state = state;
465 static int mpegps_read_header(AVFormatContext *s,
466 AVFormatParameters *ap)
468 MpegDemuxContext *m = s->priv_data;
469 m->header_state = 0xff;
470 /* no need to do more */
474 static INT64 get_pts(ByteIOContext *pb, int c)
481 pts = (INT64)((c >> 1) & 0x07) << 30;
483 pts |= (INT64)(val >> 1) << 15;
485 pts |= (INT64)(val >> 1);
489 static int mpegps_read_packet(AVFormatContext *s,
492 MpegDemuxContext *m = s->priv_data;
494 int len, size, startcode, i, c, flags, header_len, type, codec_id;
497 /* next start code (should be immediately after */
499 m->header_state = 0xff;
500 size = MAX_SYNC_SIZE;
501 startcode = find_start_code(&s->pb, &size, &m->header_state);
502 //printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
505 if (startcode == PACK_START_CODE)
507 if (startcode == SYSTEM_HEADER_START_CODE)
509 if (startcode == PADDING_STREAM ||
510 startcode == PRIVATE_STREAM_2) {
512 len = get_be16(&s->pb);
513 url_fskip(&s->pb, len);
516 /* find matching stream */
517 if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
518 (startcode >= 0x1e0 && startcode <= 0x1ef) ||
519 (startcode == 0x1bd)))
522 len = get_be16(&s->pb);
527 c = get_byte(&s->pb);
529 /* XXX: for mpeg1, should test only bit 7 */
533 if ((c & 0xc0) == 0x40) {
534 /* buffer scale & size */
536 c = get_byte(&s->pb);
539 if ((c & 0xf0) == 0x20) {
540 pts = get_pts(&s->pb, c);
543 } else if ((c & 0xf0) == 0x30) {
544 pts = get_pts(&s->pb, c);
545 dts = get_pts(&s->pb, -1);
547 } else if ((c & 0xc0) == 0x80) {
549 if ((c & 0x30) != 0) {
550 fprintf(stderr, "Encrypted multiplex not handled\n");
553 flags = get_byte(&s->pb);
554 header_len = get_byte(&s->pb);
556 if (header_len > len)
558 if ((flags & 0xc0) == 0x40) {
559 pts = get_pts(&s->pb, -1);
563 } if ((flags & 0xc0) == 0xc0) {
564 pts = get_pts(&s->pb, -1);
565 dts = get_pts(&s->pb, -1);
570 while (header_len > 0) {
575 if (startcode == 0x1bd) {
576 startcode = get_byte(&s->pb);
578 if (startcode >= 0x80 && startcode <= 0xbf) {
579 /* audio: skip header */
587 /* now find stream */
588 for(i=0;i<s->nb_streams;i++) {
590 if (st->id == startcode)
593 /* no stream found: add a new stream */
594 st = av_new_stream(s, startcode);
597 if (startcode >= 0x1e0 && startcode <= 0x1ef) {
598 type = CODEC_TYPE_VIDEO;
599 codec_id = CODEC_ID_MPEG1VIDEO;
600 } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
601 type = CODEC_TYPE_AUDIO;
602 codec_id = CODEC_ID_MP2;
603 } else if (startcode >= 0x80 && startcode <= 0x9f) {
604 type = CODEC_TYPE_AUDIO;
605 codec_id = CODEC_ID_AC3;
609 url_fskip(&s->pb, len);
612 st->codec.codec_type = type;
613 st->codec.codec_id = codec_id;
615 av_new_packet(pkt, len);
616 //printf("\nRead Packet ID: %x PTS: %f Size: %d", startcode,
617 // (float)pts/90000, len);
618 get_buffer(&s->pb, pkt->data, pkt->size);
620 pkt->stream_index = st->index;
624 static int mpegps_read_close(AVFormatContext *s)
629 static AVOutputFormat mpegps_mux = {
634 sizeof(MpegMuxContext),
638 mpeg_mux_write_packet,
642 static AVInputFormat mpegps_demux = {
645 sizeof(MpegDemuxContext),
650 flags: AVFMT_NOHEADER,
653 int mpegps_init(void)
655 av_register_output_format(&mpegps_mux);
656 av_register_input_format(&mpegps_demux);