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
28 uint8_t buffer[MAX_PAYLOAD_SIZE];
30 int nb_frames; /* number of starting frame encountered (AC3) */
31 int frame_start_offset; /* starting offset of the frame + 1 (0 if none) */
33 int max_buffer_size; /* in bytes */
37 uint8_t lpcm_header[3];
42 int packet_size; /* required packet size */
44 int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
45 int system_header_freq;
46 int system_header_size;
47 int mux_rate; /* bitrate in units of 50 bytes/s */
54 int scr_stream_index; /* stream from which the system clock is
55 computed (VBR case) */
56 int64_t last_scr; /* current system clock */
58 double vcd_padding_bitrate;
59 int64_t vcd_padding_bytes_written;
63 #define PACK_START_CODE ((unsigned int)0x000001ba)
64 #define SYSTEM_HEADER_START_CODE ((unsigned int)0x000001bb)
65 #define SEQUENCE_END_CODE ((unsigned int)0x000001b7)
66 #define PACKET_START_CODE_MASK ((unsigned int)0xffffff00)
67 #define PACKET_START_CODE_PREFIX ((unsigned int)0x00000100)
68 #define ISO_11172_END_CODE ((unsigned int)0x000001b9)
71 #define PROGRAM_STREAM_MAP 0x1bc
72 #define PRIVATE_STREAM_1 0x1bd
73 #define PADDING_STREAM 0x1be
74 #define PRIVATE_STREAM_2 0x1bf
83 static const int lpcm_freq_tab[4] = { 48000, 96000, 44100, 32000 };
85 #ifdef CONFIG_ENCODERS
86 static AVOutputFormat mpeg1system_mux;
87 static AVOutputFormat mpeg1vcd_mux;
88 static AVOutputFormat mpeg2vob_mux;
89 static AVOutputFormat mpeg2svcd_mux;
91 static int put_pack_header(AVFormatContext *ctx,
92 uint8_t *buf, int64_t timestamp)
94 MpegMuxContext *s = ctx->priv_data;
97 init_put_bits(&pb, buf, 128);
99 put_bits(&pb, 32, PACK_START_CODE);
101 put_bits(&pb, 2, 0x1);
103 put_bits(&pb, 4, 0x2);
105 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
107 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
109 put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
112 /* clock extension */
116 put_bits(&pb, 22, s->mux_rate);
120 put_bits(&pb, 5, 0x1f); /* reserved */
121 put_bits(&pb, 3, 0); /* stuffing length */
124 return pbBufPtr(&pb) - pb.buf;
127 static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id)
129 MpegMuxContext *s = ctx->priv_data;
130 int size, rate_bound, i, private_stream_coded, id;
133 init_put_bits(&pb, buf, 128);
135 put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
136 put_bits(&pb, 16, 0);
139 rate_bound = s->mux_rate; /* maximum bit rate of the multiplexed stream */
140 put_bits(&pb, 22, rate_bound);
141 put_bits(&pb, 1, 1); /* marker */
142 if (s->is_vcd && only_for_stream_id==VIDEO_ID) {
143 /* This header applies only to the video stream (see VCD standard p. IV-7)*/
146 put_bits(&pb, 6, s->audio_bound);
149 /* see VCD standard, p. IV-7*/
153 put_bits(&pb, 1, 0); /* variable bitrate*/
154 put_bits(&pb, 1, 0); /* non constrainted bit stream */
158 /* see VCD standard p IV-7 */
159 put_bits(&pb, 1, 1); /* audio locked */
160 put_bits(&pb, 1, 1); /* video locked */
162 put_bits(&pb, 1, 0); /* audio locked */
163 put_bits(&pb, 1, 0); /* video locked */
166 put_bits(&pb, 1, 1); /* marker */
168 if (s->is_vcd && only_for_stream_id==AUDIO_ID) {
169 /* This header applies only to the audio stream (see VCD standard p. IV-7)*/
172 put_bits(&pb, 5, s->video_bound);
174 put_bits(&pb, 8, 0xff); /* reserved byte */
176 /* audio stream info */
177 private_stream_coded = 0;
178 for(i=0;i<ctx->nb_streams;i++) {
179 StreamInfo *stream = ctx->streams[i]->priv_data;
181 /* For VCDs, only include the stream info for the stream
182 that the pack which contains this system belongs to.
183 (see VCD standard p. IV-7) */
184 if ( !s->is_vcd || stream->id==only_for_stream_id
185 || only_for_stream_id==0) {
189 /* special case for private streams (AC3 use that) */
190 if (private_stream_coded)
192 private_stream_coded = 1;
195 put_bits(&pb, 8, id); /* stream ID */
200 put_bits(&pb, 13, stream->max_buffer_size / 128);
204 put_bits(&pb, 13, stream->max_buffer_size / 1024);
209 size = pbBufPtr(&pb) - pb.buf;
210 /* patch packet size */
211 buf[4] = (size - 6) >> 8;
212 buf[5] = (size - 6) & 0xff;
217 static int get_system_header_size(AVFormatContext *ctx)
219 int buf_index, i, private_stream_coded;
223 private_stream_coded = 0;
224 for(i=0;i<ctx->nb_streams;i++) {
225 stream = ctx->streams[i]->priv_data;
226 if (stream->id < 0xc0) {
227 if (private_stream_coded)
229 private_stream_coded = 1;
236 static int mpeg_mux_init(AVFormatContext *ctx)
238 MpegMuxContext *s = ctx->priv_data;
239 int bitrate, i, mpa_id, mpv_id, ac3_id, dts_id, lpcm_id, j;
245 s->packet_number = 0;
246 s->is_vcd = (ctx->oformat == &mpeg1vcd_mux);
247 s->is_svcd = (ctx->oformat == &mpeg2svcd_mux);
248 s->is_mpeg2 = (ctx->oformat == &mpeg2vob_mux || ctx->oformat == &mpeg2svcd_mux);
250 if (s->is_vcd || s->is_svcd)
251 s->packet_size = 2324; /* VCD/SVCD packet size */
253 s->packet_size = 2048;
255 s->vcd_padding_bytes_written = 0;
256 s->vcd_padding_bitrate=0;
265 s->scr_stream_index = -1;
266 for(i=0;i<ctx->nb_streams;i++) {
267 st = ctx->streams[i];
268 stream = av_mallocz(sizeof(StreamInfo));
271 st->priv_data = stream;
273 switch(st->codec.codec_type) {
274 case CODEC_TYPE_AUDIO:
275 if (st->codec.codec_id == CODEC_ID_AC3) {
276 stream->id = ac3_id++;
277 } else if (st->codec.codec_id == CODEC_ID_DTS) {
278 stream->id = dts_id++;
279 } else if (st->codec.codec_id == CODEC_ID_PCM_S16BE) {
280 stream->id = lpcm_id++;
281 for(j = 0; j < 4; j++) {
282 if (lpcm_freq_tab[j] == st->codec.sample_rate)
287 if (st->codec.channels > 8)
289 stream->lpcm_header[0] = 0x0c;
290 stream->lpcm_header[1] = (st->codec.channels - 1) | (j << 4);
291 stream->lpcm_header[2] = 0x80;
292 stream->lpcm_align = st->codec.channels * 2;
294 stream->id = mpa_id++;
297 /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
298 Right now it is also used for everything else.*/
299 stream->max_buffer_size = 4 * 1024;
302 case CODEC_TYPE_VIDEO:
303 /* by default, video is used for the SCR computation */
304 if (s->scr_stream_index == -1)
305 s->scr_stream_index = i;
306 stream->id = mpv_id++;
308 /* see VCD standard, p. IV-7*/
309 stream->max_buffer_size = 46 * 1024;
311 /* This value HAS to be used for SVCD (see SVCD standard, p. 26 V.2.3.2).
312 Right now it is also used for everything else.*/
313 stream->max_buffer_size = 230 * 1024;
320 /* if no SCR, use first stream (audio) */
321 if (s->scr_stream_index == -1)
322 s->scr_stream_index = 0;
327 for(i=0;i<ctx->nb_streams;i++) {
328 st = ctx->streams[i];
329 stream = (StreamInfo*) st->priv_data;
331 bitrate += st->codec.bit_rate;
333 if (stream->id==AUDIO_ID)
334 audio_bitrate += st->codec.bit_rate;
335 else if (stream->id==VIDEO_ID)
336 video_bitrate += st->codec.bit_rate;
340 double overhead_rate;
342 /* The VCD standard mandates that the mux_rate field is 3528
343 (see standard p. IV-6).
344 The value is actually "wrong", i.e. if you calculate
345 it using the normal formula and the 75 sectors per second transfer
346 rate you get a different value because the real pack size is 2324,
347 not 2352. But the standard explicitly specifies that the mux_rate
348 field in the header must have this value.*/
349 s->mux_rate=2352 * 75 / 50; /* = 3528*/
351 /* The VCD standard states that the muxed stream must be
352 exactly 75 packs / second (the data rate of a single speed cdrom).
353 Since the video bitrate (probably 1150000 bits/sec) will be below
354 the theoretical maximum we have to add some padding packets
355 to make up for the lower data rate.
356 (cf. VCD standard p. IV-6 )*/
358 /* Add the header overhead to the data rate.
359 2279 data bytes per audio pack, 2294 data bytes per video pack*/
360 overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
361 overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
364 /* Add padding so that the full bitrate is 2324*75 bytes/sec */
365 s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate);
368 /* we increase slightly the bitrate to take into account the
369 headers. XXX: compute it exactly */
371 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
374 if (s->is_vcd || s->is_mpeg2)
376 s->pack_header_freq = 1;
378 /* every 2 seconds */
379 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
381 /* the above seems to make pack_header_freq zero sometimes */
382 if (s->pack_header_freq == 0)
383 s->pack_header_freq = 1;
386 /* every 200 packets. Need to look at the spec. */
387 s->system_header_freq = s->pack_header_freq * 40;
389 /* the standard mandates that there are only two system headers
390 in the whole file: one in the first packet of each stream.
391 (see standard p. IV-7 and IV-8) */
392 s->system_header_freq = 0x7fffffff;
394 s->system_header_freq = s->pack_header_freq * 5;
396 for(i=0;i<ctx->nb_streams;i++) {
397 stream = ctx->streams[i]->priv_data;
398 stream->buffer_ptr = 0;
399 stream->packet_number = 0;
400 stream->start_pts = AV_NOPTS_VALUE;
401 stream->start_dts = AV_NOPTS_VALUE;
403 s->system_header_size = get_system_header_size(ctx);
407 for(i=0;i<ctx->nb_streams;i++) {
408 av_free(ctx->streams[i]->priv_data);
413 static inline void put_timestamp(ByteIOContext *pb, int id, int64_t timestamp)
417 (((timestamp >> 30) & 0x07) << 1) |
419 put_be16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
420 put_be16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
424 /* return the number of padding bytes that should be inserted into
425 the multiplexed stream.*/
426 static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
428 MpegMuxContext *s = ctx->priv_data;
431 if (s->vcd_padding_bitrate > 0 && pts!=AV_NOPTS_VALUE)
433 int64_t full_pad_bytes;
435 full_pad_bytes = (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0);
436 pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written);
439 /* might happen if we have already padded to a later timestamp. This
440 can occur if another stream has already advanced further.*/
448 /* return the exact available payload size for the next packet for
449 stream 'stream_index'. 'pts' and 'dts' are only used to know if
450 timestamps are needed in the packet header. */
451 static int get_packet_payload_size(AVFormatContext *ctx, int stream_index,
452 int64_t pts, int64_t dts)
454 MpegMuxContext *s = ctx->priv_data;
458 stream = ctx->streams[stream_index]->priv_data;
461 if (((s->packet_number % s->pack_header_freq) == 0)) {
462 /* pack header size */
469 /* there is exactly one system header for each stream in a VCD MPEG,
470 One in the very first video packet and one in the very first
471 audio packet (see VCD standard p. IV-7 and IV-8).*/
473 if (stream->packet_number==0)
474 /* The system headers refer only to the stream they occur in,
475 so they have a constant size.*/
479 if ((s->packet_number % s->system_header_freq) == 0)
480 buf_index += s->system_header_size;
484 if ((s->is_vcd && stream->packet_number==0)
485 || (s->is_svcd && s->packet_number==0))
486 /* the first pack of each stream contains only the pack header,
487 the system header and some padding (see VCD standard p. IV-6)
488 Add the padding size, so that the actual payload becomes 0.*/
489 buf_index += s->packet_size - buf_index;
491 /* packet header size */
495 if (stream->packet_number==0)
496 buf_index += 3; /* PES extension */
497 buf_index += 1; /* obligatory stuffing byte */
499 if (pts != AV_NOPTS_VALUE) {
510 if (stream->id < 0xc0) {
511 /* AC3/LPCM private data header */
513 if (stream->id >= 0xa0) {
516 /* NOTE: we round the payload size to an integer number of
518 n = (s->packet_size - buf_index) % stream->lpcm_align;
520 buf_index += (stream->lpcm_align - n);
524 if (s->is_vcd && stream->id == AUDIO_ID)
525 /* The VCD standard demands that 20 zero bytes follow
526 each audio packet (see standard p. IV-8).*/
529 return s->packet_size - buf_index;
532 /* Write an MPEG padding packet header. */
533 static int put_padding_header(AVFormatContext *ctx,uint8_t* buf, int full_padding_size)
535 MpegMuxContext *s = ctx->priv_data;
536 int size = full_padding_size - 6; /* subtract header length */
538 buf[0] = (uint8_t)(PADDING_STREAM >> 24);
539 buf[1] = (uint8_t)(PADDING_STREAM >> 16);
540 buf[2] = (uint8_t)(PADDING_STREAM >> 8);
541 buf[3] = (uint8_t)(PADDING_STREAM);
542 buf[4] = (uint8_t)(size >> 8);
543 buf[5] = (uint8_t)(size & 0xff);
552 static void put_padding_packet(AVFormatContext *ctx, ByteIOContext *pb,int packet_bytes)
557 size = put_padding_header(ctx,buffer, packet_bytes);
558 put_buffer(pb, buffer, size);
559 packet_bytes -= size;
561 for(i=0;i<packet_bytes;i++)
566 /* flush the packet on stream stream_index */
567 static void flush_packet(AVFormatContext *ctx, int stream_index,
568 int64_t pts, int64_t dts, int64_t scr)
570 MpegMuxContext *s = ctx->priv_data;
571 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
573 int size, payload_size, startcode, id, stuffing_size, i, header_len;
576 int zero_trail_bytes = 0;
577 int pad_packet_bytes = 0;
579 int general_pack = 0; /*"general" pack without data specific to one stream?*/
584 printf("packet ID=%2x PTS=%0.3f\n",
590 if (((s->packet_number % s->pack_header_freq) == 0)) {
591 /* output pack and systems header if needed */
592 size = put_pack_header(ctx, buf_ptr, scr);
596 /* there is exactly one system header for each stream in a VCD MPEG,
597 One in the very first video packet and one in the very first
598 audio packet (see VCD standard p. IV-7 and IV-8).*/
600 if (stream->packet_number==0) {
601 size = put_system_header(ctx, buf_ptr, id);
605 if ((s->packet_number % s->system_header_freq) == 0) {
606 size = put_system_header(ctx, buf_ptr, 0);
611 size = buf_ptr - buffer;
612 put_buffer(&ctx->pb, buffer, size);
614 packet_size = s->packet_size - size;
616 if (s->is_vcd && id == AUDIO_ID)
617 /* The VCD standard demands that 20 zero bytes follow
618 each audio pack (see standard p. IV-8).*/
619 zero_trail_bytes += 20;
621 if ((s->is_vcd && stream->packet_number==0)
622 || (s->is_svcd && s->packet_number==0)) {
623 /* for VCD the first pack of each stream contains only the pack header,
624 the system header and lots of padding (see VCD standard p. IV-6).
625 In the case of an audio pack, 20 zero bytes are also added at
627 /* For SVCD we fill the very first pack to increase compatibility with
628 some DVD players. Not mandated by the standard.*/
630 general_pack = 1; /* the system header refers to both streams and no stream data*/
631 pad_packet_bytes = packet_size - zero_trail_bytes;
634 packet_size -= pad_packet_bytes + zero_trail_bytes;
636 if (packet_size > 0) {
638 /* packet header size */
644 if (stream->packet_number==0)
645 header_len += 3; /* PES extension */
646 header_len += 1; /* obligatory stuffing byte */
650 if (pts != AV_NOPTS_VALUE) {
660 payload_size = packet_size - header_len;
662 startcode = PRIVATE_STREAM_1;
667 startcode = 0x100 + id;
670 stuffing_size = payload_size - stream->buffer_ptr;
671 if (stuffing_size < 0)
673 if (stuffing_size > 16) { /*<=16 for MPEG-1, <=32 for MPEG-2*/
674 pad_packet_bytes += stuffing_size;
675 packet_size -= stuffing_size;
676 payload_size -= stuffing_size;
680 put_be32(&ctx->pb, startcode);
682 put_be16(&ctx->pb, packet_size);
685 for(i=0;i<stuffing_size;i++)
686 put_byte(&ctx->pb, 0xff);
689 put_byte(&ctx->pb, 0x80); /* mpeg2 id */
693 if (pts != AV_NOPTS_VALUE) {
699 /* Both the MPEG-2 and the SVCD standards demand that the
700 P-STD_buffer_size field be included in the first packet of
701 every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
702 and MPEG-2 standard 2.7.7) */
703 if (stream->packet_number == 0)
706 put_byte(&ctx->pb, pes_flags); /* flags */
707 put_byte(&ctx->pb, header_len - 3 + stuffing_size);
709 if (pes_flags & 0x80) /*write pts*/
710 put_timestamp(&ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
711 if (pes_flags & 0x40) /*write dts*/
712 put_timestamp(&ctx->pb, 0x01, dts);
714 if (pes_flags & 0x01) { /*write pes extension*/
715 put_byte(&ctx->pb, 0x10); /* flags */
717 /* P-STD buffer info */
719 put_be16(&ctx->pb, 0x4000 | stream->max_buffer_size/128);
721 put_be16(&ctx->pb, 0x6000 | stream->max_buffer_size/1024);
725 if (pts != AV_NOPTS_VALUE) {
727 put_timestamp(&ctx->pb, 0x03, pts);
728 put_timestamp(&ctx->pb, 0x01, dts);
730 put_timestamp(&ctx->pb, 0x02, pts);
733 put_byte(&ctx->pb, 0x0f);
738 /* special stuffing byte that is always written
739 to prevent accidental generation of start codes. */
740 put_byte(&ctx->pb, 0xff);
742 for(i=0;i<stuffing_size;i++)
743 put_byte(&ctx->pb, 0xff);
746 if (startcode == PRIVATE_STREAM_1) {
747 put_byte(&ctx->pb, id);
749 /* LPCM (XXX: check nb_frames) */
750 put_byte(&ctx->pb, 7);
751 put_be16(&ctx->pb, 4); /* skip 3 header bytes */
752 put_byte(&ctx->pb, stream->lpcm_header[0]);
753 put_byte(&ctx->pb, stream->lpcm_header[1]);
754 put_byte(&ctx->pb, stream->lpcm_header[2]);
757 put_byte(&ctx->pb, stream->nb_frames);
758 put_be16(&ctx->pb, stream->frame_start_offset);
763 put_buffer(&ctx->pb, stream->buffer, payload_size - stuffing_size);
766 if (pad_packet_bytes > 0)
767 put_padding_packet(ctx,&ctx->pb, pad_packet_bytes);
769 for(i=0;i<zero_trail_bytes;i++)
770 put_byte(&ctx->pb, 0x00);
772 put_flush_packet(&ctx->pb);
776 /* only increase the stream packet number if this pack actually contains
777 something that is specific to this stream! I.e. a dedicated header
780 stream->packet_number++;
781 stream->nb_frames = 0;
782 stream->frame_start_offset = 0;
785 static void put_vcd_padding_sector(AVFormatContext *ctx)
787 /* There are two ways to do this padding: writing a sector/pack
788 of 0 values, or writing an MPEG padding pack. Both seem to
789 work with most decoders, BUT the VCD standard only allows a 0-sector
790 (see standard p. IV-4, IV-5).
791 So a 0-sector it is...*/
793 MpegMuxContext *s = ctx->priv_data;
796 for(i=0;i<s->packet_size;i++)
797 put_byte(&ctx->pb, 0);
799 s->vcd_padding_bytes_written += s->packet_size;
801 put_flush_packet(&ctx->pb);
803 /* increasing the packet number is correct. The SCR of the following packs
804 is calculated from the packet_number and it has to include the padding
805 sector (it represents the sector index, not the MPEG pack index)
806 (see VCD standard p. IV-6)*/
810 static int64_t update_scr(AVFormatContext *ctx,int stream_index,int64_t pts)
812 MpegMuxContext *s = ctx->priv_data;
818 /* Since the data delivery rate is constant, SCR is computed
819 using the formula C + i * 1200 where C is the start constant
820 and i is the pack index.
821 It is recommended that SCR 0 is at the beginning of the VCD front
822 margin (a sequence of empty Form 2 sectors on the CD).
823 It is recommended that the front margin is 30 sectors long, so
824 we use C = 30*1200 = 36000
825 (Note that even if the front margin is not 30 sectors the file
826 will still be correct according to the standard. It just won't have
827 the "recommended" value).*/
828 scr = 36000 + s->packet_number * 1200;
832 for(i=0;i<ctx->nb_streams;i++) {
833 stream = ctx->streams[i]->priv_data;
835 if(scr > stream->start_pts && stream->start_pts!=AV_NOPTS_VALUE) {
836 av_log(ctx, AV_LOG_DEBUG, "mpeg vcd: SCR above PTS (scr=%0.3f, stream index=%d, stream_pts=%0.3f).\n", scr/90000.0, i, stream->start_pts / 90000.0);
844 /* XXX I believe this calculation of SCR is wrong. SCR
845 specifies at which time the data should enter the decoder.
846 Two packs cannot enter the decoder at the same time. */
848 /* XXX: system clock should be computed precisely, especially for
849 CBR case. The current mode gives at least something coherent */
850 if (stream_index == s->scr_stream_index
851 && pts != AV_NOPTS_VALUE)
856 /* "Sanity hack": make sure that the SCR does not overtake the pts of
857 buffered data that is still waiting to be written.*/
858 for(i=0;i<ctx->nb_streams;i++) {
859 stream = ctx->streams[i]->priv_data;
861 if(scr > stream->start_pts && stream->start_pts!=AV_NOPTS_VALUE) {
862 /* av_log(ctx, AV_LOG_DEBUG, "mpeg: restricting scr to stream pts (scr=%0.3f, stream index=%d, stream_pts=%0.3f).\n", scr/90000.0, i, stream->start_pts / 90000.0); */
863 scr = stream->start_pts;
874 static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
876 MpegMuxContext *s = ctx->priv_data;
877 int stream_index= pkt->stream_index;
879 uint8_t *buf= pkt->data;
880 AVStream *st = ctx->streams[stream_index];
881 StreamInfo *stream = st->priv_data;
882 int64_t pts, dts, new_start_pts, new_start_dts;
889 /* offset pts and dts slightly into the future to be able
890 to do the compatibility fix below.*/
891 pts = (pts + 2) & ((1LL << 33) - 1);
892 dts = (dts + 2) & ((1LL << 33) - 1);
894 if (stream->packet_number == 0 && dts == pts)
895 /* For the very first packet we want to force the DTS to be included.
896 This increases compatibility with lots of DVD players.
897 Since the MPEG-2 standard mandates that DTS is only written when
898 it is different from PTS we have to move it slightly into the past.*/
899 dts = (dts - 2) & ((1LL << 33) - 1);
902 /* We have to offset the PTS, so that it is consistent with the SCR.
903 SCR starts at 36000, but the first two packs contain only padding
904 and the first pack from the other stream, respectively, may also have
906 So the real data starts at SCR 36000+3*1200. */
907 pts = (pts + 36000 + 3600) & ((1LL << 33) - 1);
908 dts = (dts + 36000 + 3600) & ((1LL << 33) - 1);
912 update_scr(ctx,stream_index,pts);
914 printf("%d: pts=%0.3f dts=%0.3f scr=%0.3f\n",
918 s->last_scr / 90000.0);
921 /* we assume here that pts != AV_NOPTS_VALUE */
922 new_start_pts = stream->start_pts;
923 new_start_dts = stream->start_dts;
925 if (stream->start_pts == AV_NOPTS_VALUE) {
929 avail_size = get_packet_payload_size(ctx, stream_index,
932 if (stream->buffer_ptr >= avail_size) {
934 update_scr(ctx,stream_index,stream->start_pts);
936 /* unlikely case: outputing the pts or dts increase the packet
937 size so that we cannot write the start of the next
938 packet. In this case, we must flush the current packet with
940 Note: this always happens for the first audio and video packet
941 in a VCD file, since they do not carry any data.*/
942 flush_packet(ctx, stream_index,
943 stream->start_pts, stream->start_dts, s->last_scr);
944 stream->buffer_ptr = 0;
946 stream->start_pts = new_start_pts;
947 stream->start_dts = new_start_dts;
949 if (stream->frame_start_offset == 0)
950 stream->frame_start_offset = stream->buffer_ptr + 1;
952 avail_size = get_packet_payload_size(ctx, stream_index,
955 len = avail_size - stream->buffer_ptr;
958 memcpy(stream->buffer + stream->buffer_ptr, buf, len);
959 stream->buffer_ptr += len;
962 if (stream->buffer_ptr >= avail_size) {
964 update_scr(ctx,stream_index,stream->start_pts);
966 /* if packet full, we send it now */
967 flush_packet(ctx, stream_index,
968 stream->start_pts, stream->start_dts, s->last_scr);
969 stream->buffer_ptr = 0;
972 /* Write one or more padding sectors, if necessary, to reach
973 the constant overall bitrate.*/
976 while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->start_pts) ) >= s->packet_size)
977 put_vcd_padding_sector(ctx);
980 /* Make sure only the FIRST pes packet for this frame has
982 stream->start_pts = AV_NOPTS_VALUE;
983 stream->start_dts = AV_NOPTS_VALUE;
990 static int mpeg_mux_end(AVFormatContext *ctx)
992 MpegMuxContext *s = ctx->priv_data;
996 /* flush each packet */
997 for(i=0;i<ctx->nb_streams;i++) {
998 stream = ctx->streams[i]->priv_data;
999 if (stream->buffer_ptr > 0) {
1000 update_scr(ctx,i,stream->start_pts);
1002 /* NOTE: we can always write the remaining data as it was
1003 tested before in mpeg_mux_write_packet() */
1004 flush_packet(ctx, i, stream->start_pts, stream->start_dts,
1009 /* End header according to MPEG1 systems standard. We do not write
1010 it as it is usually not needed by decoders and because it
1011 complicates MPEG stream concatenation. */
1012 //put_be32(&ctx->pb, ISO_11172_END_CODE);
1013 //put_flush_packet(&ctx->pb);
1015 for(i=0;i<ctx->nb_streams;i++)
1016 av_freep(&ctx->streams[i]->priv_data);
1020 #endif //CONFIG_ENCODERS
1022 /*********************************************/
1025 #define MAX_SYNC_SIZE 100000
1027 static int mpegps_probe(AVProbeData *p)
1032 /* we search the first start code. If it is a packet start code,
1033 then we decide it is mpeg ps. We do not send highest value to
1034 give a chance to mpegts */
1035 /* NOTE: the search range was restricted to avoid too many false
1038 if (p->buf_size < 6)
1041 for (i = 0; i < 20; i++) {
1043 code = (code << 8) | c;
1044 if ((code & 0xffffff00) == 0x100) {
1045 if (code == PACK_START_CODE ||
1046 code == SYSTEM_HEADER_START_CODE ||
1047 (code >= 0x1e0 && code <= 0x1ef) ||
1048 (code >= 0x1c0 && code <= 0x1df) ||
1049 code == PRIVATE_STREAM_2 ||
1050 code == PROGRAM_STREAM_MAP ||
1051 code == PRIVATE_STREAM_1 ||
1052 code == PADDING_STREAM)
1053 return AVPROBE_SCORE_MAX - 2;
1062 typedef struct MpegDemuxContext {
1066 static int mpegps_read_header(AVFormatContext *s,
1067 AVFormatParameters *ap)
1069 MpegDemuxContext *m = s->priv_data;
1070 m->header_state = 0xff;
1071 s->ctx_flags |= AVFMTCTX_NOHEADER;
1073 /* no need to do more */
1077 static int64_t get_pts(ByteIOContext *pb, int c)
1084 pts = (int64_t)((c >> 1) & 0x07) << 30;
1086 pts |= (int64_t)(val >> 1) << 15;
1088 pts |= (int64_t)(val >> 1);
1092 static int find_next_start_code(ByteIOContext *pb, int *size_ptr,
1093 uint32_t *header_state)
1095 unsigned int state, v;
1098 state = *header_state;
1105 if (state == 0x000001) {
1106 state = ((state << 8) | v) & 0xffffff;
1110 state = ((state << 8) | v) & 0xffffff;
1114 *header_state = state;
1120 static int find_prev_start_code(ByteIOContext *pb, int *size_ptr)
1122 int64_t pos, pos_start;
1123 int max_size, start_code;
1125 max_size = *size_ptr;
1126 pos_start = url_ftell(pb);
1128 /* in order to go faster, we fill the buffer */
1129 pos = pos_start - 16386;
1132 url_fseek(pb, pos, SEEK_SET);
1138 if (pos < 0 || (pos_start - pos) >= max_size) {
1142 url_fseek(pb, pos, SEEK_SET);
1143 start_code = get_be32(pb);
1144 if ((start_code & 0xffffff00) == 0x100)
1148 *size_ptr = pos_start - pos;
1152 /* read the next PES header. Return its position in ppos
1153 (if not NULL), and its start code, pts and dts.
1155 static int mpegps_read_pes_header(AVFormatContext *s,
1156 int64_t *ppos, int *pstart_code,
1157 int64_t *ppts, int64_t *pdts)
1159 MpegDemuxContext *m = s->priv_data;
1160 int len, size, startcode, c, flags, header_len;
1161 int64_t pts, dts, last_pos;
1165 /* next start code (should be immediately after) */
1166 m->header_state = 0xff;
1167 size = MAX_SYNC_SIZE;
1168 startcode = find_next_start_code(&s->pb, &size, &m->header_state);
1169 //printf("startcode=%x pos=0x%Lx\n", startcode, url_ftell(&s->pb));
1172 if (startcode == PACK_START_CODE)
1174 if (startcode == SYSTEM_HEADER_START_CODE)
1176 if (startcode == PADDING_STREAM ||
1177 startcode == PRIVATE_STREAM_2) {
1179 len = get_be16(&s->pb);
1180 url_fskip(&s->pb, len);
1183 /* find matching stream */
1184 if (!((startcode >= 0x1c0 && startcode <= 0x1df) ||
1185 (startcode >= 0x1e0 && startcode <= 0x1ef) ||
1186 (startcode == 0x1bd)))
1189 *ppos = url_ftell(&s->pb) - 4;
1191 len = get_be16(&s->pb);
1192 pts = AV_NOPTS_VALUE;
1193 dts = AV_NOPTS_VALUE;
1198 c = get_byte(&s->pb);
1200 /* XXX: for mpeg1, should test only bit 7 */
1204 if ((c & 0xc0) == 0x40) {
1205 /* buffer scale & size */
1209 c = get_byte(&s->pb);
1212 if ((c & 0xf0) == 0x20) {
1215 dts = pts = get_pts(&s->pb, c);
1217 } else if ((c & 0xf0) == 0x30) {
1220 pts = get_pts(&s->pb, c);
1221 dts = get_pts(&s->pb, -1);
1223 } else if ((c & 0xc0) == 0x80) {
1225 if ((c & 0x30) != 0) {
1226 /* Encrypted multiplex not handled */
1229 flags = get_byte(&s->pb);
1230 header_len = get_byte(&s->pb);
1232 if (header_len > len)
1234 if ((flags & 0xc0) == 0x80) {
1235 dts = pts = get_pts(&s->pb, -1);
1240 } if ((flags & 0xc0) == 0xc0) {
1241 pts = get_pts(&s->pb, -1);
1242 dts = get_pts(&s->pb, -1);
1243 if (header_len < 10)
1249 while (header_len > 0) {
1257 if (startcode == 0x1bd) {
1260 startcode = get_byte(&s->pb);
1262 if (startcode >= 0x80 && startcode <= 0xbf) {
1263 /* audio: skip header */
1272 if(dts != AV_NOPTS_VALUE && ppos){
1274 for(i=0; i<s->nb_streams; i++){
1275 if(startcode == s->streams[i]->id) {
1276 av_add_index_entry(s->streams[i], *ppos, dts, 0, 0 /* FIXME keyframe? */);
1281 *pstart_code = startcode;
1287 static int mpegps_read_packet(AVFormatContext *s,
1291 int len, startcode, i, type, codec_id = 0;
1292 int64_t pts, dts, dummy_pos; //dummy_pos is needed for the index building to work
1295 len = mpegps_read_pes_header(s, &dummy_pos, &startcode, &pts, &dts);
1299 /* now find stream */
1300 for(i=0;i<s->nb_streams;i++) {
1302 if (st->id == startcode)
1305 if (startcode >= 0x1e0 && startcode <= 0x1ef) {
1306 type = CODEC_TYPE_VIDEO;
1307 codec_id = CODEC_ID_MPEG2VIDEO;
1308 } else if (startcode >= 0x1c0 && startcode <= 0x1df) {
1309 type = CODEC_TYPE_AUDIO;
1310 codec_id = CODEC_ID_MP2;
1311 } else if (startcode >= 0x80 && startcode <= 0x89) {
1312 type = CODEC_TYPE_AUDIO;
1313 codec_id = CODEC_ID_AC3;
1314 } else if (startcode >= 0x8a && startcode <= 0x9f) {
1315 type = CODEC_TYPE_AUDIO;
1316 codec_id = CODEC_ID_DTS;
1317 } else if (startcode >= 0xa0 && startcode <= 0xbf) {
1318 type = CODEC_TYPE_AUDIO;
1319 codec_id = CODEC_ID_PCM_S16BE;
1323 url_fskip(&s->pb, len);
1326 /* no stream found: add a new stream */
1327 st = av_new_stream(s, startcode);
1330 st->codec.codec_type = type;
1331 st->codec.codec_id = codec_id;
1332 if (codec_id != CODEC_ID_PCM_S16BE)
1333 st->need_parsing = 1;
1335 if (startcode >= 0xa0 && startcode <= 0xbf) {
1338 /* for LPCM, we just skip the header and consider it is raw
1342 get_byte(&s->pb); /* emphasis (1), muse(1), reserved(1), frame number(5) */
1343 b1 = get_byte(&s->pb); /* quant (2), freq(2), reserved(1), channels(3) */
1344 get_byte(&s->pb); /* dynamic range control (0x80 = off) */
1346 freq = (b1 >> 4) & 3;
1347 st->codec.sample_rate = lpcm_freq_tab[freq];
1348 st->codec.channels = 1 + (b1 & 7);
1349 st->codec.bit_rate = st->codec.channels * st->codec.sample_rate * 2;
1351 av_new_packet(pkt, len);
1352 get_buffer(&s->pb, pkt->data, pkt->size);
1355 pkt->stream_index = st->index;
1357 av_log(s, AV_LOG_DEBUG, "%d: pts=%0.3f dts=%0.3f\n",
1358 pkt->stream_index, pkt->pts / 90000.0, pkt->dts / 90000.0);
1364 static int mpegps_read_close(AVFormatContext *s)
1369 static int64_t mpegps_read_dts(AVFormatContext *s, int stream_index,
1370 int64_t *ppos, int64_t pos_limit)
1373 int64_t pos, pts, dts;
1377 printf("read_dts: pos=0x%llx next=%d -> ", pos, find_next);
1379 url_fseek(&s->pb, pos, SEEK_SET);
1381 len = mpegps_read_pes_header(s, &pos, &startcode, &pts, &dts);
1384 printf("none (ret=%d)\n", len);
1386 return AV_NOPTS_VALUE;
1388 if (startcode == s->streams[stream_index]->id &&
1389 dts != AV_NOPTS_VALUE) {
1392 url_fskip(&s->pb, len);
1395 printf("pos=0x%llx dts=0x%llx %0.3f\n", pos, dts, dts / 90000.0);
1401 #ifdef CONFIG_ENCODERS
1402 static AVOutputFormat mpeg1system_mux = {
1404 "MPEG1 System format",
1407 sizeof(MpegMuxContext),
1409 CODEC_ID_MPEG1VIDEO,
1411 mpeg_mux_write_packet,
1415 static AVOutputFormat mpeg1vcd_mux = {
1417 "MPEG1 System format (VCD)",
1420 sizeof(MpegMuxContext),
1422 CODEC_ID_MPEG1VIDEO,
1424 mpeg_mux_write_packet,
1428 static AVOutputFormat mpeg2vob_mux = {
1430 "MPEG2 PS format (VOB)",
1433 sizeof(MpegMuxContext),
1435 CODEC_ID_MPEG2VIDEO,
1437 mpeg_mux_write_packet,
1441 /* Same as mpeg2vob_mux except that the pack size is 2324 */
1442 static AVOutputFormat mpeg2svcd_mux = {
1444 "MPEG2 PS format (VOB)",
1447 sizeof(MpegMuxContext),
1449 CODEC_ID_MPEG2VIDEO,
1451 mpeg_mux_write_packet,
1457 #endif //CONFIG_ENCODERS
1459 AVInputFormat mpegps_demux = {
1462 sizeof(MpegDemuxContext),
1467 NULL, //mpegps_read_seek,
1471 int mpegps_init(void)
1473 #ifdef CONFIG_ENCODERS
1474 av_register_output_format(&mpeg1system_mux);
1475 av_register_output_format(&mpeg1vcd_mux);
1476 av_register_output_format(&mpeg2vob_mux);
1477 av_register_output_format(&mpeg2svcd_mux);
1478 #endif //CONFIG_ENCODERS
1479 av_register_input_format(&mpegps_demux);