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
21 #define MAX_PAYLOAD_SIZE 4096
26 uint8_t buffer[MAX_PAYLOAD_SIZE];
29 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 */
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 #ifdef CONFIG_ENCODERS
66 extern AVOutputFormat mpeg1system_mux;
67 extern AVOutputFormat mpeg1vcd_mux;
68 extern AVOutputFormat mpeg2vob_mux;
70 static int put_pack_header(AVFormatContext *ctx,
71 uint8_t *buf, int64_t timestamp)
73 MpegMuxContext *s = ctx->priv_data;
76 init_put_bits(&pb, buf, 128);
78 put_bits(&pb, 32, PACK_START_CODE);
80 put_bits(&pb, 2, 0x1);
82 put_bits(&pb, 4, 0x2);
84 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
86 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
88 put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
96 put_bits(&pb, 22, s->mux_rate);
99 put_bits(&pb, 5, 0x1f); /* reserved */
100 put_bits(&pb, 3, 0); /* stuffing length */
103 return pbBufPtr(&pb) - pb.buf;
106 static int put_system_header(AVFormatContext *ctx, uint8_t *buf)
108 MpegMuxContext *s = ctx->priv_data;
109 int size, rate_bound, i, private_stream_coded, id;
112 init_put_bits(&pb, buf, 128);
114 put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
115 put_bits(&pb, 16, 0);
118 rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */
119 put_bits(&pb, 22, rate_bound);
120 put_bits(&pb, 1, 1); /* marker */
121 put_bits(&pb, 6, s->audio_bound);
123 put_bits(&pb, 1, 1); /* variable bitrate */
124 put_bits(&pb, 1, 1); /* non constrainted bit stream */
126 put_bits(&pb, 1, 0); /* audio locked */
127 put_bits(&pb, 1, 0); /* video locked */
128 put_bits(&pb, 1, 1); /* marker */
130 put_bits(&pb, 5, s->video_bound);
131 put_bits(&pb, 8, 0xff); /* reserved byte */
133 /* audio stream info */
134 private_stream_coded = 0;
135 for(i=0;i<ctx->nb_streams;i++) {
136 StreamInfo *stream = ctx->streams[i]->priv_data;
139 /* special case for private streams (AC3 use that) */
140 if (private_stream_coded)
142 private_stream_coded = 1;
145 put_bits(&pb, 8, id); /* stream ID */
150 put_bits(&pb, 13, stream->max_buffer_size / 128);
154 put_bits(&pb, 13, stream->max_buffer_size / 1024);
158 size = pbBufPtr(&pb) - pb.buf;
159 /* patch packet size */
160 buf[4] = (size - 6) >> 8;
161 buf[5] = (size - 6) & 0xff;
166 static int mpeg_mux_init(AVFormatContext *ctx)
168 MpegMuxContext *s = ctx->priv_data;
169 int bitrate, i, mpa_id, mpv_id, ac3_id;
173 s->packet_number = 0;
174 s->is_vcd = (ctx->oformat == &mpeg1vcd_mux);
175 s->is_mpeg2 = (ctx->oformat == &mpeg2vob_mux);
178 s->packet_size = 2324; /* VCD packet size */
180 s->packet_size = 2048;
182 /* startcode(4) + length(2) + flags(1) */
183 s->packet_data_max_size = s->packet_size - 7;
189 for(i=0;i<ctx->nb_streams;i++) {
190 st = ctx->streams[i];
191 stream = av_mallocz(sizeof(StreamInfo));
194 st->priv_data = stream;
196 switch(st->codec.codec_type) {
197 case CODEC_TYPE_AUDIO:
198 if (st->codec.codec_id == CODEC_ID_AC3)
199 stream->id = ac3_id++;
201 stream->id = mpa_id++;
202 stream->max_buffer_size = 4 * 1024;
205 case CODEC_TYPE_VIDEO:
206 stream->id = mpv_id++;
207 stream->max_buffer_size = 46 * 1024;
215 /* we increase slightly the bitrate to take into account the
216 headers. XXX: compute it exactly */
218 for(i=0;i<ctx->nb_streams;i++) {
219 st = ctx->streams[i];
220 bitrate += st->codec.bit_rate;
222 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
224 if (s->is_vcd || s->is_mpeg2)
226 s->pack_header_freq = 1;
228 /* every 2 seconds */
229 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
231 /* the above seems to make pack_header_freq zero sometimes */
232 if (s->pack_header_freq == 0)
233 s->pack_header_freq = 1;
236 /* every 200 packets. Need to look at the spec. */
237 s->system_header_freq = s->pack_header_freq * 40;
239 /* every 40 packets, this is my invention */
240 s->system_header_freq = s->pack_header_freq * 40;
242 s->system_header_freq = s->pack_header_freq * 5;
244 for(i=0;i<ctx->nb_streams;i++) {
245 stream = ctx->streams[i]->priv_data;
246 stream->buffer_ptr = 0;
247 stream->packet_number = 0;
248 stream->start_pts = -1;
252 for(i=0;i<ctx->nb_streams;i++) {
253 av_free(ctx->streams[i]->priv_data);
258 /* flush the packet on stream stream_index */
259 static void flush_packet(AVFormatContext *ctx, int stream_index)
261 MpegMuxContext *s = ctx->priv_data;
262 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
264 int size, payload_size, startcode, id, len, stuffing_size, i, header_len;
269 timestamp = stream->start_pts;
272 printf("packet ID=%2x PTS=%0.3f\n",
273 id, timestamp / 90000.0);
277 if (((s->packet_number % s->pack_header_freq) == 0)) {
278 /* output pack and systems header if needed */
279 size = put_pack_header(ctx, buf_ptr, timestamp);
281 if ((s->packet_number % s->system_header_freq) == 0) {
282 size = put_system_header(ctx, buf_ptr);
286 size = buf_ptr - buffer;
287 put_buffer(&ctx->pb, buffer, size);
295 payload_size = s->packet_size - (size + 6 + header_len);
297 startcode = PRIVATE_STREAM_1;
300 startcode = 0x100 + id;
302 stuffing_size = payload_size - stream->buffer_ptr;
303 if (stuffing_size < 0)
306 put_be32(&ctx->pb, startcode);
308 put_be16(&ctx->pb, payload_size + header_len);
310 for(i=0;i<stuffing_size;i++)
311 put_byte(&ctx->pb, 0xff);
314 put_byte(&ctx->pb, 0x80); /* mpeg2 id */
315 put_byte(&ctx->pb, 0x80); /* flags */
316 put_byte(&ctx->pb, 0x05); /* header len (only pts is included) */
320 (((timestamp >> 30) & 0x07) << 1) |
322 put_be16(&ctx->pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
323 put_be16(&ctx->pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
325 if (startcode == PRIVATE_STREAM_1) {
326 put_byte(&ctx->pb, id);
327 if (id >= 0x80 && id <= 0xbf) {
328 /* XXX: need to check AC3 spec */
329 put_byte(&ctx->pb, 1);
330 put_byte(&ctx->pb, 0);
331 put_byte(&ctx->pb, 2);
336 put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size);
337 put_flush_packet(&ctx->pb);
339 /* preserve remaining data */
340 len = stream->buffer_ptr - payload_size;
343 memmove(stream->buffer, stream->buffer + stream->buffer_ptr - len, len);
344 stream->buffer_ptr = len;
347 stream->packet_number++;
348 stream->start_pts = -1;
351 static int mpeg_mux_write_packet(AVFormatContext *ctx, int stream_index,
352 const uint8_t *buf, int size, int64_t pts)
354 MpegMuxContext *s = ctx->priv_data;
355 AVStream *st = ctx->streams[stream_index];
356 StreamInfo *stream = st->priv_data;
361 if (stream->start_pts == -1) {
362 stream->start_pts = pts;
364 len = s->packet_data_max_size - stream->buffer_ptr;
367 memcpy(stream->buffer + stream->buffer_ptr, buf, len);
368 stream->buffer_ptr += len;
371 while (stream->buffer_ptr >= s->packet_data_max_size) {
372 /* output the packet */
373 if (stream->start_pts == -1)
374 stream->start_pts = pts;
375 flush_packet(ctx, stream_index);
381 static int mpeg_mux_end(AVFormatContext *ctx)
386 /* flush each packet */
387 for(i=0;i<ctx->nb_streams;i++) {
388 stream = ctx->streams[i]->priv_data;
389 if (stream->buffer_ptr > 0) {
390 flush_packet(ctx, i);
394 /* End header according to MPEG1 systems standard. We do not write
395 it as it is usually not needed by decoders and because it
396 complicates MPEG stream concatenation. */
397 //put_be32(&ctx->pb, ISO_11172_END_CODE);
398 //put_flush_packet(&ctx->pb);
400 for(i=0;i<ctx->nb_streams;i++)
401 av_freep(&ctx->streams[i]->priv_data);
405 #endif //CONFIG_ENCODERS
407 /*********************************************/
410 #define MAX_SYNC_SIZE 100000
412 static int mpegps_probe(AVProbeData *p)
417 /* we search the first start code. If it is a packet start code,
418 then we decide it is mpeg ps. We do not send highest value to
419 give a chance to mpegts */
420 /* NOTE: the search range was restricted to avoid too many false
426 for (i = 0; i < 20; i++) {
428 code = (code << 8) | c;
429 if ((code & 0xffffff00) == 0x100) {
430 if (code == PACK_START_CODE ||
431 code == SYSTEM_HEADER_START_CODE ||
432 (code >= 0x1e0 && code <= 0x1ef) ||
433 (code >= 0x1c0 && code <= 0x1df) ||
434 code == PRIVATE_STREAM_2 ||
435 code == PROGRAM_STREAM_MAP ||
436 code == PRIVATE_STREAM_1 ||
437 code == PADDING_STREAM)
438 return AVPROBE_SCORE_MAX - 2;
447 typedef struct MpegDemuxContext {
451 static int mpegps_read_header(AVFormatContext *s,
452 AVFormatParameters *ap)
454 MpegDemuxContext *m = s->priv_data;
455 m->header_state = 0xff;
456 s->ctx_flags |= AVFMTCTX_NOHEADER;
458 /* no need to do more */
462 static int64_t get_pts(ByteIOContext *pb, int c)
469 pts = (int64_t)((c >> 1) & 0x07) << 30;
471 pts |= (int64_t)(val >> 1) << 15;
473 pts |= (int64_t)(val >> 1);
477 static int find_next_start_code(ByteIOContext *pb, int *size_ptr,
478 uint32_t *header_state)
480 unsigned int state, v;
483 state = *header_state;
490 if (state == 0x000001) {
491 state = ((state << 8) | v) & 0xffffff;
495 state = ((state << 8) | v) & 0xffffff;
499 *header_state = state;
505 static int find_prev_start_code(ByteIOContext *pb, int *size_ptr)
507 int64_t pos, pos_start;
508 int max_size, start_code;
510 max_size = *size_ptr;
511 pos_start = url_ftell(pb);
513 /* in order to go faster, we fill the buffer */
514 pos = pos_start - 16386;
517 url_fseek(pb, pos, SEEK_SET);
523 if (pos < 0 || (pos_start - pos) >= max_size) {
527 url_fseek(pb, pos, SEEK_SET);
528 start_code = get_be32(pb);
529 if ((start_code & 0xffffff00) == 0x100)
533 *size_ptr = pos_start - pos;
537 /* read the next (or previous) PES header. Return its position in ppos
538 (if not NULL), and its start code, pts and dts.
540 static int mpegps_read_pes_header(AVFormatContext *s,
541 int64_t *ppos, int *pstart_code,
542 int64_t *ppts, int64_t *pdts, int find_next)
544 MpegDemuxContext *m = s->priv_data;
545 int len, size, startcode, c, flags, header_len;
546 int64_t pts, dts, last_pos;
551 /* next start code (should be immediately after) */
552 m->header_state = 0xff;
553 size = MAX_SYNC_SIZE;
554 startcode = find_next_start_code(&s->pb, &size, &m->header_state);
557 url_fseek(&s->pb, last_pos, SEEK_SET);
558 size = MAX_SYNC_SIZE;
559 startcode = find_prev_start_code(&s->pb, &size);
560 last_pos = url_ftell(&s->pb) - 4;
562 //printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
565 if (startcode == PACK_START_CODE)
567 if (startcode == SYSTEM_HEADER_START_CODE)
569 if (startcode == PADDING_STREAM ||
570 startcode == PRIVATE_STREAM_2) {
572 len = get_be16(&s->pb);
573 url_fskip(&s->pb, len);
576 /* find matching stream */
577 if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
578 (startcode >= 0x1e0 && startcode <= 0x1ef) ||
579 (startcode == 0x1bd)))
582 *ppos = url_ftell(&s->pb) - 4;
584 len = get_be16(&s->pb);
585 pts = AV_NOPTS_VALUE;
586 dts = AV_NOPTS_VALUE;
591 c = get_byte(&s->pb);
593 /* XXX: for mpeg1, should test only bit 7 */
597 if ((c & 0xc0) == 0x40) {
598 /* buffer scale & size */
602 c = get_byte(&s->pb);
605 if ((c & 0xf0) == 0x20) {
608 dts = pts = get_pts(&s->pb, c);
610 } else if ((c & 0xf0) == 0x30) {
613 pts = get_pts(&s->pb, c);
614 dts = get_pts(&s->pb, -1);
616 } else if ((c & 0xc0) == 0x80) {
618 if ((c & 0x30) != 0) {
619 /* Encrypted multiplex not handled */
622 flags = get_byte(&s->pb);
623 header_len = get_byte(&s->pb);
625 if (header_len > len)
627 if ((flags & 0xc0) == 0x80) {
628 dts = pts = get_pts(&s->pb, -1);
633 } if ((flags & 0xc0) == 0xc0) {
634 pts = get_pts(&s->pb, -1);
635 dts = get_pts(&s->pb, -1);
642 while (header_len > 0) {
647 if (startcode == 0x1bd) {
650 startcode = get_byte(&s->pb);
652 if (startcode >= 0x80 && startcode <= 0xbf) {
653 /* audio: skip header */
662 *pstart_code = startcode;
668 static int mpegps_read_packet(AVFormatContext *s,
672 int len, startcode, i, type, codec_id;
676 len = mpegps_read_pes_header(s, NULL, &startcode, &pts, &dts, 1);
680 /* now find stream */
681 for(i=0;i<s->nb_streams;i++) {
683 if (st->id == startcode)
686 if (startcode >= 0x1e0 && startcode <= 0x1ef) {
687 type = CODEC_TYPE_VIDEO;
688 codec_id = CODEC_ID_MPEG1VIDEO;
689 } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
690 type = CODEC_TYPE_AUDIO;
691 codec_id = CODEC_ID_MP2;
692 } else if (startcode >= 0x80 && startcode <= 0x9f) {
693 type = CODEC_TYPE_AUDIO;
694 codec_id = CODEC_ID_AC3;
695 } else if (startcode >= 0xa0 && startcode <= 0xbf) {
696 type = CODEC_TYPE_AUDIO;
697 codec_id = CODEC_ID_PCM_S16BE;
701 url_fskip(&s->pb, len);
704 /* no stream found: add a new stream */
705 st = av_new_stream(s, startcode);
708 st->codec.codec_type = type;
709 st->codec.codec_id = codec_id;
710 if (codec_id != CODEC_ID_PCM_S16BE)
711 st->need_parsing = 1;
713 if (startcode >= 0xa0 && startcode <= 0xbf) {
715 static const int lpcm_freq_tab[4] = { 48000, 96000, 44100, 32000 };
717 /* for LPCM, we just skip the header and consider it is raw
721 get_byte(&s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
722 b1 = get_byte(&s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
723 get_byte(&s->pb); /* dynamic range control (0x80 = off) */
725 freq = (b1 >> 4) & 3;
726 st->codec.sample_rate = lpcm_freq_tab[freq];
727 st->codec.channels = 1 + (b1 & 7);
728 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate * 2;
730 av_new_packet(pkt, len);
731 //printf("\nRead Packet ID: %x PTS: %f Size: %d", startcode,
732 // (float)pts/90000, len);
733 get_buffer(&s->pb, pkt->data, pkt->size);
736 pkt->stream_index = st->index;
740 static int mpegps_read_close(AVFormatContext *s)
745 static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
746 int64_t *ppos, int find_next)
749 int64_t pos, pts, dts;
753 printf("read_dts: pos=0x%llx next=%d -> ", pos, find_next);
755 url_fseek(&s->pb, pos, SEEK_SET);
757 len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts, find_next);
760 printf("none (ret=%d)\n", len);
762 return AV_NOPTS_VALUE;
764 if (startcode == s->streams[stream_index]->id &&
765 dts != AV_NOPTS_VALUE) {
769 url_fskip(&s->pb, len);
771 url_fseek(&s->pb, pos, SEEK_SET);
775 printf("pos=0x%llx dts=0x%llx %0.3f\n", pos, dts, dts / 90000.0);
781 static int find_stream_index(AVFormatContext *s)
786 if (s->nb_streams <= 0)
788 for(i = 0; i < s->nb_streams; i++) {
790 if (st->codec.codec_type == CODEC_TYPE_VIDEO) {
797 static int mpegps_read_seek(AVFormatContext *s,
798 int stream_index, int64_t timestamp)
800 int64_t pos_min, pos_max, pos;
801 int64_t dts_min, dts_max, dts;
803 timestamp = (timestamp * 90000) / AV_TIME_BASE;
806 printf("read_seek: %d %0.3f\n", stream_index, timestamp / 90000.0);
809 /* XXX: find stream_index by looking at the first PES packet found */
810 if (stream_index < 0) {
811 stream_index = find_stream_index(s);
812 if (stream_index < 0)
816 dts_min = mpegps_read_dts(s, stream_index, &pos_min, 1);
817 if (dts_min == AV_NOPTS_VALUE) {
818 /* we can reach this case only if no PTS are present in
822 pos_max = url_filesize(url_fileno(&s->pb)) - 1;
823 dts_max = mpegps_read_dts(s, stream_index, &pos_max, 0);
825 while (pos_min <= pos_max) {
827 printf("pos_min=0x%llx pos_max=0x%llx dts_min=%0.3f dts_max=%0.3f\n",
829 dts_min / 90000.0, dts_max / 90000.0);
831 if (timestamp <= dts_min) {
834 } else if (timestamp >= dts_max) {
838 /* interpolate position (better than dichotomy) */
839 pos = (int64_t)((double)(pos_max - pos_min) *
840 (double)(timestamp - dts_min) /
841 (double)(dts_max - dts_min)) + pos_min;
844 printf("pos=0x%llx\n", pos);
846 /* read the next timestamp */
847 dts = mpegps_read_dts(s, stream_index, &pos, 1);
848 /* check if we are lucky */
849 if (dts == AV_NOPTS_VALUE) {
850 /* should never happen */
853 } else if (timestamp == dts) {
855 } else if (timestamp < dts) {
857 dts_max = mpegps_read_dts(s, stream_index, &pos_max, 0);
858 if (dts_max == AV_NOPTS_VALUE) {
859 /* should never happen */
861 } else if (timestamp >= dts_max) {
867 dts_min = mpegps_read_dts(s, stream_index, &pos_min, 1);
868 if (dts_min == AV_NOPTS_VALUE) {
869 /* should never happen */
871 } else if (timestamp <= dts_min) {
880 dts_min = mpegps_read_dts(s, stream_index, &pos_min, 1);
882 dts_max = mpegps_read_dts(s, stream_index, &pos_min, 1);
883 printf("pos=0x%llx %0.3f<=%0.3f<=%0.3f\n",
884 pos, dts_min / 90000.0, timestamp / 90000.0, dts_max / 90000.0);
887 url_fseek(&s->pb, pos, SEEK_SET);
891 #ifdef CONFIG_ENCODERS
892 static AVOutputFormat mpeg1system_mux = {
894 "MPEG1 System format",
897 sizeof(MpegMuxContext),
901 mpeg_mux_write_packet,
905 static AVOutputFormat mpeg1vcd_mux = {
907 "MPEG1 System format (VCD)",
910 sizeof(MpegMuxContext),
914 mpeg_mux_write_packet,
918 static AVOutputFormat mpeg2vob_mux = {
920 "MPEG2 PS format (VOB)",
923 sizeof(MpegMuxContext),
927 mpeg_mux_write_packet,
930 #endif //CONFIG_ENCODERS
932 AVInputFormat mpegps_demux = {
935 sizeof(MpegDemuxContext),
943 int mpegps_init(void)
945 #ifdef CONFIG_ENCODERS
946 av_register_output_format(&mpeg1system_mux);
947 av_register_output_format(&mpeg1vcd_mux);
948 av_register_output_format(&mpeg2vob_mux);
949 #endif //CONFIG_ENCODERS
950 av_register_input_format(&mpegps_demux);