3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "allformats.h"
24 #include "bitstream.h"
28 #define MAX_PAYLOAD_SIZE 4096
34 typedef struct PacketDesc {
40 struct PacketDesc *next;
46 int max_buffer_size; /* in bytes */
48 PacketDesc *predecode_packet;
49 PacketDesc *premux_packet;
50 PacketDesc **next_packet;
52 uint8_t lpcm_header[3];
56 int64_t vobu_start_pts;
60 int packet_size; /* required packet size */
62 int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
63 int system_header_freq;
64 int system_header_size;
65 int mux_rate; /* bitrate in units of 50 bytes/s */
73 int64_t last_scr; /* current system clock */
75 double vcd_padding_bitrate; //FIXME floats
76 int64_t vcd_padding_bytes_written;
80 static int put_pack_header(AVFormatContext *ctx,
81 uint8_t *buf, int64_t timestamp)
83 MpegMuxContext *s = ctx->priv_data;
86 init_put_bits(&pb, buf, 128);
88 put_bits(&pb, 32, PACK_START_CODE);
90 put_bits(&pb, 2, 0x1);
92 put_bits(&pb, 4, 0x2);
94 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
96 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
98 put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
101 /* clock extension */
105 put_bits(&pb, 22, s->mux_rate);
109 put_bits(&pb, 5, 0x1f); /* reserved */
110 put_bits(&pb, 3, 0); /* stuffing length */
113 return pbBufPtr(&pb) - pb.buf;
116 static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id)
118 MpegMuxContext *s = ctx->priv_data;
119 int size, i, private_stream_coded, id;
122 init_put_bits(&pb, buf, 128);
124 put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
125 put_bits(&pb, 16, 0);
128 put_bits(&pb, 22, s->mux_rate); /* maximum bit rate of the multiplexed stream */
129 put_bits(&pb, 1, 1); /* marker */
130 if (s->is_vcd && only_for_stream_id==VIDEO_ID) {
131 /* This header applies only to the video stream (see VCD standard p. IV-7)*/
134 put_bits(&pb, 6, s->audio_bound);
137 /* see VCD standard, p. IV-7*/
141 put_bits(&pb, 1, 0); /* variable bitrate*/
142 put_bits(&pb, 1, 0); /* non constrainted bit stream */
145 if (s->is_vcd || s->is_dvd) {
146 /* see VCD standard p IV-7 */
147 put_bits(&pb, 1, 1); /* audio locked */
148 put_bits(&pb, 1, 1); /* video locked */
150 put_bits(&pb, 1, 0); /* audio locked */
151 put_bits(&pb, 1, 0); /* video locked */
154 put_bits(&pb, 1, 1); /* marker */
156 if (s->is_vcd && only_for_stream_id==AUDIO_ID) {
157 /* This header applies only to the audio stream (see VCD standard p. IV-7)*/
160 put_bits(&pb, 5, s->video_bound);
163 put_bits(&pb, 1, 0); /* packet_rate_restriction_flag */
164 put_bits(&pb, 7, 0x7f); /* reserved byte */
166 put_bits(&pb, 8, 0xff); /* reserved byte */
168 /* DVD-Video Stream_bound entries
169 id (0xB9) video, maximum P-STD for stream 0xE0. (P-STD_buffer_bound_scale = 1)
170 id (0xB8) audio, maximum P-STD for any MPEG audio (0xC0 to 0xC7) streams. If there are none set to 4096 (32x128). (P-STD_buffer_bound_scale = 0)
171 id (0xBD) private stream 1 (audio other than MPEG and subpictures). (P-STD_buffer_bound_scale = 1)
172 id (0xBF) private stream 2, NAV packs, set to 2x1024. */
175 int P_STD_max_video = 0;
176 int P_STD_max_mpeg_audio = 0;
177 int P_STD_max_mpeg_PS1 = 0;
179 for(i=0;i<ctx->nb_streams;i++) {
180 StreamInfo *stream = ctx->streams[i]->priv_data;
183 if (id == 0xbd && stream->max_buffer_size > P_STD_max_mpeg_PS1) {
184 P_STD_max_mpeg_PS1 = stream->max_buffer_size;
185 } else if (id >= 0xc0 && id <= 0xc7 && stream->max_buffer_size > P_STD_max_mpeg_audio) {
186 P_STD_max_mpeg_audio = stream->max_buffer_size;
187 } else if (id == 0xe0 && stream->max_buffer_size > P_STD_max_video) {
188 P_STD_max_video = stream->max_buffer_size;
193 put_bits(&pb, 8, 0xb9); /* stream ID */
196 put_bits(&pb, 13, P_STD_max_video / 1024);
199 if (P_STD_max_mpeg_audio == 0)
200 P_STD_max_mpeg_audio = 4096;
201 put_bits(&pb, 8, 0xb8); /* stream ID */
204 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
206 /* private stream 1 */
207 put_bits(&pb, 8, 0xbd); /* stream ID */
210 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
212 /* private stream 2 */
213 put_bits(&pb, 8, 0xbf); /* stream ID */
216 put_bits(&pb, 13, 2);
219 /* audio stream info */
220 private_stream_coded = 0;
221 for(i=0;i<ctx->nb_streams;i++) {
222 StreamInfo *stream = ctx->streams[i]->priv_data;
225 /* For VCDs, only include the stream info for the stream
226 that the pack which contains this system belongs to.
227 (see VCD standard p. IV-7) */
228 if ( !s->is_vcd || stream->id==only_for_stream_id
229 || only_for_stream_id==0) {
233 /* special case for private streams (AC3 use that) */
234 if (private_stream_coded)
236 private_stream_coded = 1;
239 put_bits(&pb, 8, id); /* stream ID */
244 put_bits(&pb, 13, stream->max_buffer_size / 128);
248 put_bits(&pb, 13, stream->max_buffer_size / 1024);
255 size = pbBufPtr(&pb) - pb.buf;
256 /* patch packet size */
257 buf[4] = (size - 6) >> 8;
258 buf[5] = (size - 6) & 0xff;
263 static int get_system_header_size(AVFormatContext *ctx)
265 int buf_index, i, private_stream_coded;
267 MpegMuxContext *s = ctx->priv_data;
270 return 18; // DVD-Video system headers are 18 bytes fixed length.
273 private_stream_coded = 0;
274 for(i=0;i<ctx->nb_streams;i++) {
275 stream = ctx->streams[i]->priv_data;
276 if (stream->id < 0xc0) {
277 if (private_stream_coded)
279 private_stream_coded = 1;
286 static int mpeg_mux_init(AVFormatContext *ctx)
288 MpegMuxContext *s = ctx->priv_data;
289 int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j;
295 s->packet_number = 0;
296 s->is_vcd = (ENABLE_MPEG1VCD_MUXER && ctx->oformat == &mpeg1vcd_muxer);
297 s->is_svcd = (ENABLE_MPEG2SVCD_MUXER && ctx->oformat == &mpeg2svcd_muxer);
298 s->is_mpeg2 = (ENABLE_MPEG2VOB_MUXER && ctx->oformat == &mpeg2vob_muxer ||
299 ENABLE_MPEG2DVD_MUXER && ctx->oformat == &mpeg2dvd_muxer ||
300 ENABLE_MPEG2SVCD_MUXER && ctx->oformat == &mpeg2svcd_muxer);
301 s->is_dvd = (ENABLE_MPEG2DVD_MUXER && ctx->oformat == &mpeg2dvd_muxer);
304 s->packet_size = ctx->packet_size;
306 s->packet_size = 2048;
308 s->vcd_padding_bytes_written = 0;
309 s->vcd_padding_bitrate=0;
319 for(i=0;i<ctx->nb_streams;i++) {
320 st = ctx->streams[i];
321 stream = av_mallocz(sizeof(StreamInfo));
324 st->priv_data = stream;
326 av_set_pts_info(st, 64, 1, 90000);
328 switch(st->codec->codec_type) {
329 case CODEC_TYPE_AUDIO:
330 if (st->codec->codec_id == CODEC_ID_AC3) {
331 stream->id = ac3_id++;
332 } else if (st->codec->codec_id == CODEC_ID_DTS) {
333 stream->id = dts_id++;
334 } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
335 stream->id = lpcm_id++;
336 for(j = 0; j < 4; j++) {
337 if (lpcm_freq_tab[j] == st->codec->sample_rate)
342 if (st->codec->channels > 8)
344 stream->lpcm_header[0] = 0x0c;
345 stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4);
346 stream->lpcm_header[2] = 0x80;
347 stream->lpcm_align = st->codec->channels * 2;
349 stream->id = mpa_id++;
352 /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
353 Right now it is also used for everything else.*/
354 stream->max_buffer_size = 4 * 1024;
357 case CODEC_TYPE_VIDEO:
358 stream->id = mpv_id++;
359 if (st->codec->rc_buffer_size)
360 stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8;
362 stream->max_buffer_size = 230*1024; //FIXME this is probably too small as default
364 /* see VCD standard, p. IV-7*/
365 stream->max_buffer_size = 46 * 1024;
367 /* This value HAS to be used for SVCD (see SVCD standard, p. 26 V.2.3.2).
368 Right now it is also used for everything else.*/
369 stream->max_buffer_size = 230 * 1024;
373 case CODEC_TYPE_SUBTITLE:
374 stream->id = mps_id++;
375 stream->max_buffer_size = 16 * 1024;
380 av_fifo_init(&stream->fifo, 16);
385 for(i=0;i<ctx->nb_streams;i++) {
387 st = ctx->streams[i];
388 stream = (StreamInfo*) st->priv_data;
390 if(st->codec->rc_max_rate || stream->id==VIDEO_ID)
391 codec_rate= st->codec->rc_max_rate;
393 codec_rate= st->codec->bit_rate;
396 codec_rate= (1<<21)*8*50/ctx->nb_streams;
398 bitrate += codec_rate;
400 if (stream->id==AUDIO_ID)
401 audio_bitrate += codec_rate;
402 else if (stream->id==VIDEO_ID)
403 video_bitrate += codec_rate;
407 s->mux_rate= (ctx->mux_rate + (8 * 50) - 1) / (8 * 50);
409 /* we increase slightly the bitrate to take into account the
410 headers. XXX: compute it exactly */
411 bitrate += bitrate*5/100;
413 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
417 double overhead_rate;
419 /* The VCD standard mandates that the mux_rate field is 3528
420 (see standard p. IV-6).
421 The value is actually "wrong", i.e. if you calculate
422 it using the normal formula and the 75 sectors per second transfer
423 rate you get a different value because the real pack size is 2324,
424 not 2352. But the standard explicitly specifies that the mux_rate
425 field in the header must have this value.*/
426 // s->mux_rate=2352 * 75 / 50; /* = 3528*/
428 /* The VCD standard states that the muxed stream must be
429 exactly 75 packs / second (the data rate of a single speed cdrom).
430 Since the video bitrate (probably 1150000 bits/sec) will be below
431 the theoretical maximum we have to add some padding packets
432 to make up for the lower data rate.
433 (cf. VCD standard p. IV-6 )*/
435 /* Add the header overhead to the data rate.
436 2279 data bytes per audio pack, 2294 data bytes per video pack*/
437 overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
438 overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
441 /* Add padding so that the full bitrate is 2324*75 bytes/sec */
442 s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate);
445 if (s->is_vcd || s->is_mpeg2)
447 s->pack_header_freq = 1;
449 /* every 2 seconds */
450 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
452 /* the above seems to make pack_header_freq zero sometimes */
453 if (s->pack_header_freq == 0)
454 s->pack_header_freq = 1;
457 /* every 200 packets. Need to look at the spec. */
458 s->system_header_freq = s->pack_header_freq * 40;
460 /* the standard mandates that there are only two system headers
461 in the whole file: one in the first packet of each stream.
462 (see standard p. IV-7 and IV-8) */
463 s->system_header_freq = 0x7fffffff;
465 s->system_header_freq = s->pack_header_freq * 5;
467 for(i=0;i<ctx->nb_streams;i++) {
468 stream = ctx->streams[i]->priv_data;
469 stream->packet_number = 0;
471 s->system_header_size = get_system_header_size(ctx);
475 for(i=0;i<ctx->nb_streams;i++) {
476 av_free(ctx->streams[i]->priv_data);
478 return AVERROR(ENOMEM);
481 static inline void put_timestamp(ByteIOContext *pb, int id, int64_t timestamp)
485 (((timestamp >> 30) & 0x07) << 1) |
487 put_be16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
488 put_be16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
492 /* return the number of padding bytes that should be inserted into
493 the multiplexed stream.*/
494 static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
496 MpegMuxContext *s = ctx->priv_data;
499 if (s->vcd_padding_bitrate > 0 && pts!=AV_NOPTS_VALUE)
501 int64_t full_pad_bytes;
503 full_pad_bytes = (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0); //FIXME this is wrong
504 pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written);
507 /* might happen if we have already padded to a later timestamp. This
508 can occur if another stream has already advanced further.*/
516 #if 0 /* unused, remove? */
517 /* return the exact available payload size for the next packet for
518 stream 'stream_index'. 'pts' and 'dts' are only used to know if
519 timestamps are needed in the packet header. */
520 static int get_packet_payload_size(AVFormatContext *ctx, int stream_index,
521 int64_t pts, int64_t dts)
523 MpegMuxContext *s = ctx->priv_data;
527 stream = ctx->streams[stream_index]->priv_data;
530 if (((s->packet_number % s->pack_header_freq) == 0)) {
531 /* pack header size */
538 /* there is exactly one system header for each stream in a VCD MPEG,
539 One in the very first video packet and one in the very first
540 audio packet (see VCD standard p. IV-7 and IV-8).*/
542 if (stream->packet_number==0)
543 /* The system headers refer only to the stream they occur in,
544 so they have a constant size.*/
548 if ((s->packet_number % s->system_header_freq) == 0)
549 buf_index += s->system_header_size;
553 if ((s->is_vcd && stream->packet_number==0)
554 || (s->is_svcd && s->packet_number==0))
555 /* the first pack of each stream contains only the pack header,
556 the system header and some padding (see VCD standard p. IV-6)
557 Add the padding size, so that the actual payload becomes 0.*/
558 buf_index += s->packet_size - buf_index;
560 /* packet header size */
564 if (stream->packet_number==0)
565 buf_index += 3; /* PES extension */
566 buf_index += 1; /* obligatory stuffing byte */
568 if (pts != AV_NOPTS_VALUE) {
579 if (stream->id < 0xc0) {
580 /* AC3/LPCM private data header */
582 if (stream->id >= 0xa0) {
585 /* NOTE: we round the payload size to an integer number of
587 n = (s->packet_size - buf_index) % stream->lpcm_align;
589 buf_index += (stream->lpcm_align - n);
593 if (s->is_vcd && stream->id == AUDIO_ID)
594 /* The VCD standard demands that 20 zero bytes follow
595 each audio packet (see standard p. IV-8).*/
598 return s->packet_size - buf_index;
602 /* Write an MPEG padding packet header. */
603 static void put_padding_packet(AVFormatContext *ctx, ByteIOContext *pb,int packet_bytes)
605 MpegMuxContext *s = ctx->priv_data;
608 put_be32(pb, PADDING_STREAM);
609 put_be16(pb, packet_bytes - 6);
616 for(i=0;i<packet_bytes;i++)
620 static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len){
622 PacketDesc *pkt_desc= stream->premux_packet;
625 if(pkt_desc->size == pkt_desc->unwritten_size)
627 len -= pkt_desc->unwritten_size;
628 pkt_desc= pkt_desc->next;
634 /* flush the packet on stream stream_index */
635 static int flush_packet(AVFormatContext *ctx, int stream_index,
636 int64_t pts, int64_t dts, int64_t scr, int trailer_size)
638 MpegMuxContext *s = ctx->priv_data;
639 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
641 int size, payload_size, startcode, id, stuffing_size, i, header_len;
644 int zero_trail_bytes = 0;
645 int pad_packet_bytes = 0;
647 int general_pack = 0; /*"general" pack without data specific to one stream?*/
653 printf("packet ID=%2x PTS=%0.3f\n",
659 if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) {
660 /* output pack and systems header if needed */
661 size = put_pack_header(ctx, buf_ptr, scr);
666 /* there is exactly one system header for each stream in a VCD MPEG,
667 One in the very first video packet and one in the very first
668 audio packet (see VCD standard p. IV-7 and IV-8).*/
670 if (stream->packet_number==0) {
671 size = put_system_header(ctx, buf_ptr, id);
674 } else if (s->is_dvd) {
675 if (stream->align_iframe || s->packet_number == 0){
676 int PES_bytes_to_fill = s->packet_size - size - 10;
678 if (pts != AV_NOPTS_VALUE) {
680 PES_bytes_to_fill -= 5 + 5;
682 PES_bytes_to_fill -= 5;
685 if (stream->bytes_to_iframe == 0 || s->packet_number == 0) {
686 size = put_system_header(ctx, buf_ptr, 0);
688 size = buf_ptr - buffer;
689 put_buffer(&ctx->pb, buffer, size);
691 put_be32(&ctx->pb, PRIVATE_STREAM_2);
692 put_be16(&ctx->pb, 0x03d4); // length
693 put_byte(&ctx->pb, 0x00); // substream ID, 00=PCI
694 for (i = 0; i < 979; i++)
695 put_byte(&ctx->pb, 0x00);
697 put_be32(&ctx->pb, PRIVATE_STREAM_2);
698 put_be16(&ctx->pb, 0x03fa); // length
699 put_byte(&ctx->pb, 0x01); // substream ID, 01=DSI
700 for (i = 0; i < 1017; i++)
701 put_byte(&ctx->pb, 0x00);
703 memset(buffer, 0, 128);
706 stream->align_iframe = 0;
707 scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
708 size = put_pack_header(ctx, buf_ptr, scr);
712 } else if (stream->bytes_to_iframe < PES_bytes_to_fill) {
713 pad_packet_bytes = PES_bytes_to_fill - stream->bytes_to_iframe;
717 if ((s->packet_number % s->system_header_freq) == 0) {
718 size = put_system_header(ctx, buf_ptr, 0);
723 size = buf_ptr - buffer;
724 put_buffer(&ctx->pb, buffer, size);
726 packet_size = s->packet_size - size;
728 if (s->is_vcd && id == AUDIO_ID)
729 /* The VCD standard demands that 20 zero bytes follow
730 each audio pack (see standard p. IV-8).*/
731 zero_trail_bytes += 20;
733 if ((s->is_vcd && stream->packet_number==0)
734 || (s->is_svcd && s->packet_number==0)) {
735 /* for VCD the first pack of each stream contains only the pack header,
736 the system header and lots of padding (see VCD standard p. IV-6).
737 In the case of an audio pack, 20 zero bytes are also added at
739 /* For SVCD we fill the very first pack to increase compatibility with
740 some DVD players. Not mandated by the standard.*/
742 general_pack = 1; /* the system header refers to both streams and no stream data*/
743 pad_packet_bytes = packet_size - zero_trail_bytes;
746 packet_size -= pad_packet_bytes + zero_trail_bytes;
748 if (packet_size > 0) {
750 /* packet header size */
756 if (stream->packet_number==0)
757 header_len += 3; /* PES extension */
758 header_len += 1; /* obligatory stuffing byte */
762 if (pts != AV_NOPTS_VALUE) {
772 payload_size = packet_size - header_len;
774 startcode = PRIVATE_STREAM_1;
782 startcode = 0x100 + id;
785 stuffing_size = payload_size - av_fifo_size(&stream->fifo);
787 // first byte does not fit -> reset pts/dts + stuffing
788 if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){
792 if(pts != AV_NOPTS_VALUE)
793 timestamp_len += s->is_mpeg2 ? 5 : 4;
794 pts=dts= AV_NOPTS_VALUE;
795 header_len -= timestamp_len;
796 if (s->is_dvd && stream->align_iframe) {
797 pad_packet_bytes += timestamp_len;
798 packet_size -= timestamp_len;
800 payload_size += timestamp_len;
802 stuffing_size += timestamp_len;
803 if(payload_size > trailer_size)
804 stuffing_size += payload_size - trailer_size;
807 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) { // can't use padding, so use stuffing
808 packet_size += pad_packet_bytes;
809 payload_size += pad_packet_bytes; // undo the previous adjustment
810 if (stuffing_size < 0) {
811 stuffing_size = pad_packet_bytes;
813 stuffing_size += pad_packet_bytes;
815 pad_packet_bytes = 0;
818 if (stuffing_size < 0)
820 if (stuffing_size > 16) { /*<=16 for MPEG-1, <=32 for MPEG-2*/
821 pad_packet_bytes += stuffing_size;
822 packet_size -= stuffing_size;
823 payload_size -= stuffing_size;
827 nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size);
829 put_be32(&ctx->pb, startcode);
831 put_be16(&ctx->pb, packet_size);
834 for(i=0;i<stuffing_size;i++)
835 put_byte(&ctx->pb, 0xff);
838 put_byte(&ctx->pb, 0x80); /* mpeg2 id */
842 if (pts != AV_NOPTS_VALUE) {
848 /* Both the MPEG-2 and the SVCD standards demand that the
849 P-STD_buffer_size field be included in the first packet of
850 every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
851 and MPEG-2 standard 2.7.7) */
852 if (stream->packet_number == 0)
855 put_byte(&ctx->pb, pes_flags); /* flags */
856 put_byte(&ctx->pb, header_len - 3 + stuffing_size);
858 if (pes_flags & 0x80) /*write pts*/
859 put_timestamp(&ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
860 if (pes_flags & 0x40) /*write dts*/
861 put_timestamp(&ctx->pb, 0x01, dts);
863 if (pes_flags & 0x01) { /*write pes extension*/
864 put_byte(&ctx->pb, 0x10); /* flags */
866 /* P-STD buffer info */
868 put_be16(&ctx->pb, 0x4000 | stream->max_buffer_size/128);
870 put_be16(&ctx->pb, 0x6000 | stream->max_buffer_size/1024);
874 if (pts != AV_NOPTS_VALUE) {
876 put_timestamp(&ctx->pb, 0x03, pts);
877 put_timestamp(&ctx->pb, 0x01, dts);
879 put_timestamp(&ctx->pb, 0x02, pts);
882 put_byte(&ctx->pb, 0x0f);
887 /* special stuffing byte that is always written
888 to prevent accidental generation of start codes. */
889 put_byte(&ctx->pb, 0xff);
891 for(i=0;i<stuffing_size;i++)
892 put_byte(&ctx->pb, 0xff);
895 if (startcode == PRIVATE_STREAM_1) {
896 put_byte(&ctx->pb, id);
898 /* LPCM (XXX: check nb_frames) */
899 put_byte(&ctx->pb, 7);
900 put_be16(&ctx->pb, 4); /* skip 3 header bytes */
901 put_byte(&ctx->pb, stream->lpcm_header[0]);
902 put_byte(&ctx->pb, stream->lpcm_header[1]);
903 put_byte(&ctx->pb, stream->lpcm_header[2]);
904 } else if (id >= 0x40) {
906 put_byte(&ctx->pb, nb_frames);
907 put_be16(&ctx->pb, trailer_size+1);
912 if(av_fifo_generic_read(&stream->fifo, payload_size - stuffing_size, &put_buffer, &ctx->pb) < 0)
914 stream->bytes_to_iframe -= payload_size - stuffing_size;
920 if (pad_packet_bytes > 0)
921 put_padding_packet(ctx,&ctx->pb, pad_packet_bytes);
923 for(i=0;i<zero_trail_bytes;i++)
924 put_byte(&ctx->pb, 0x00);
926 put_flush_packet(&ctx->pb);
930 /* only increase the stream packet number if this pack actually contains
931 something that is specific to this stream! I.e. a dedicated header
934 stream->packet_number++;
936 return payload_size - stuffing_size;
939 static void put_vcd_padding_sector(AVFormatContext *ctx)
941 /* There are two ways to do this padding: writing a sector/pack
942 of 0 values, or writing an MPEG padding pack. Both seem to
943 work with most decoders, BUT the VCD standard only allows a 0-sector
944 (see standard p. IV-4, IV-5).
945 So a 0-sector it is...*/
947 MpegMuxContext *s = ctx->priv_data;
950 for(i=0;i<s->packet_size;i++)
951 put_byte(&ctx->pb, 0);
953 s->vcd_padding_bytes_written += s->packet_size;
955 put_flush_packet(&ctx->pb);
957 /* increasing the packet number is correct. The SCR of the following packs
958 is calculated from the packet_number and it has to include the padding
959 sector (it represents the sector index, not the MPEG pack index)
960 (see VCD standard p. IV-6)*/
964 #if 0 /* unused, remove? */
965 static int64_t get_vcd_scr(AVFormatContext *ctx,int stream_index,int64_t pts)
967 MpegMuxContext *s = ctx->priv_data;
970 /* Since the data delivery rate is constant, SCR is computed
971 using the formula C + i * 1200 where C is the start constant
972 and i is the pack index.
973 It is recommended that SCR 0 is at the beginning of the VCD front
974 margin (a sequence of empty Form 2 sectors on the CD).
975 It is recommended that the front margin is 30 sectors long, so
976 we use C = 30*1200 = 36000
977 (Note that even if the front margin is not 30 sectors the file
978 will still be correct according to the standard. It just won't have
979 the "recommended" value).*/
980 scr = 36000 + s->packet_number * 1200;
986 static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){
987 // MpegMuxContext *s = ctx->priv_data;
990 for(i=0; i<ctx->nb_streams; i++){
991 AVStream *st = ctx->streams[i];
992 StreamInfo *stream = st->priv_data;
993 PacketDesc *pkt_desc;
995 while((pkt_desc= stream->predecode_packet)
996 && scr > pkt_desc->dts){ //FIXME > vs >=
997 if(stream->buffer_index < pkt_desc->size ||
998 stream->predecode_packet == stream->premux_packet){
999 av_log(ctx, AV_LOG_ERROR,
1000 "buffer underflow i=%d bufi=%d size=%d\n",
1001 i, stream->buffer_index, pkt_desc->size);
1004 stream->buffer_index -= pkt_desc->size;
1006 stream->predecode_packet= pkt_desc->next;
1007 av_freep(&pkt_desc);
1014 static int output_packet(AVFormatContext *ctx, int flush){
1015 MpegMuxContext *s = ctx->priv_data;
1018 int i, avail_space, es_size, trailer_size;
1020 int best_score= INT_MIN;
1021 int ignore_constraints=0;
1022 int64_t scr= s->last_scr;
1023 PacketDesc *timestamp_packet;
1024 const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
1027 for(i=0; i<ctx->nb_streams; i++){
1028 AVStream *st = ctx->streams[i];
1029 StreamInfo *stream = st->priv_data;
1030 const int avail_data= av_fifo_size(&stream->fifo);
1031 const int space= stream->max_buffer_size - stream->buffer_index;
1032 int rel_space= 1024*space / stream->max_buffer_size;
1033 PacketDesc *next_pkt= stream->premux_packet;
1035 /* for subtitle, a single PES packet must be generated,
1036 so we flush after every single subtitle packet */
1037 if(s->packet_size > avail_data && !flush
1038 && st->codec->codec_type != CODEC_TYPE_SUBTITLE)
1042 assert(avail_data>0);
1044 if(space < s->packet_size && !ignore_constraints)
1047 if(next_pkt && next_pkt->dts - scr > max_delay)
1050 if(rel_space > best_score){
1051 best_score= rel_space;
1058 int64_t best_dts= INT64_MAX;
1060 for(i=0; i<ctx->nb_streams; i++){
1061 AVStream *st = ctx->streams[i];
1062 StreamInfo *stream = st->priv_data;
1063 PacketDesc *pkt_desc= stream->predecode_packet;
1064 if(pkt_desc && pkt_desc->dts < best_dts)
1065 best_dts= pkt_desc->dts;
1069 av_log(ctx, AV_LOG_DEBUG, "bumping scr, scr:%f, dts:%f\n",
1070 scr/90000.0, best_dts/90000.0);
1072 if(best_dts == INT64_MAX)
1075 if(scr >= best_dts+1 && !ignore_constraints){
1076 av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n");
1077 ignore_constraints= 1;
1079 scr= FFMAX(best_dts+1, scr);
1080 if(remove_decoded_packets(ctx, scr) < 0)
1085 assert(best_i >= 0);
1087 st = ctx->streams[best_i];
1088 stream = st->priv_data;
1090 assert(av_fifo_size(&stream->fifo) > 0);
1092 assert(avail_space >= s->packet_size || ignore_constraints);
1094 timestamp_packet= stream->premux_packet;
1095 if(timestamp_packet->unwritten_size == timestamp_packet->size){
1098 trailer_size= timestamp_packet->unwritten_size;
1099 timestamp_packet= timestamp_packet->next;
1102 if(timestamp_packet){
1103 //av_log(ctx, AV_LOG_DEBUG, "dts:%f pts:%f scr:%f stream:%d\n", timestamp_packet->dts/90000.0, timestamp_packet->pts/90000.0, scr/90000.0, best_i);
1104 es_size= flush_packet(ctx, best_i, timestamp_packet->pts, timestamp_packet->dts, scr, trailer_size);
1106 assert(av_fifo_size(&stream->fifo) == trailer_size);
1107 es_size= flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr, trailer_size);
1111 /* Write one or more padding sectors, if necessary, to reach
1112 the constant overall bitrate.*/
1115 while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->premux_packet->pts) ) >= s->packet_size){ //FIXME pts cannot be correct here
1116 put_vcd_padding_sector(ctx);
1117 s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
1121 stream->buffer_index += es_size;
1122 s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
1124 while(stream->premux_packet && stream->premux_packet->unwritten_size <= es_size){
1125 es_size -= stream->premux_packet->unwritten_size;
1126 stream->premux_packet= stream->premux_packet->next;
1129 stream->premux_packet->unwritten_size -= es_size;
1131 if(remove_decoded_packets(ctx, s->last_scr) < 0)
1137 static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
1139 MpegMuxContext *s = ctx->priv_data;
1140 int stream_index= pkt->stream_index;
1141 int size= pkt->size;
1142 uint8_t *buf= pkt->data;
1143 AVStream *st = ctx->streams[stream_index];
1144 StreamInfo *stream = st->priv_data;
1146 PacketDesc *pkt_desc;
1147 const int preload= av_rescale(ctx->preload, 90000, AV_TIME_BASE);
1148 const int is_iframe = st->codec->codec_type == CODEC_TYPE_VIDEO && (pkt->flags & PKT_FLAG_KEY);
1153 if(pts != AV_NOPTS_VALUE) pts += preload;
1154 if(dts != AV_NOPTS_VALUE) dts += preload;
1156 //av_log(ctx, AV_LOG_DEBUG, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n", dts/90000.0, pts/90000.0, pkt->flags, pkt->stream_index, pts != AV_NOPTS_VALUE);
1157 if (!stream->premux_packet)
1158 stream->next_packet = &stream->premux_packet;
1159 *stream->next_packet=
1160 pkt_desc= av_mallocz(sizeof(PacketDesc));
1163 pkt_desc->unwritten_size=
1164 pkt_desc->size= size;
1165 if(!stream->predecode_packet)
1166 stream->predecode_packet= pkt_desc;
1167 stream->next_packet= &pkt_desc->next;
1169 av_fifo_realloc(&stream->fifo, av_fifo_size(&stream->fifo) + size + 1);
1172 if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) { // min VOBU length 0.4 seconds (mpucoder)
1173 stream->bytes_to_iframe = av_fifo_size(&stream->fifo);
1174 stream->align_iframe = 1;
1175 stream->vobu_start_pts = pts;
1179 av_fifo_write(&stream->fifo, buf, size);
1182 int ret= output_packet(ctx, 0);
1188 static int mpeg_mux_end(AVFormatContext *ctx)
1190 // MpegMuxContext *s = ctx->priv_data;
1195 int ret= output_packet(ctx, 1);
1202 /* End header according to MPEG1 systems standard. We do not write
1203 it as it is usually not needed by decoders and because it
1204 complicates MPEG stream concatenation. */
1205 //put_be32(&ctx->pb, ISO_11172_END_CODE);
1206 //put_flush_packet(&ctx->pb);
1208 for(i=0;i<ctx->nb_streams;i++) {
1209 stream = ctx->streams[i]->priv_data;
1211 assert(av_fifo_size(&stream->fifo) == 0);
1212 av_fifo_free(&stream->fifo);
1217 #ifdef CONFIG_MPEG1SYSTEM_MUXER
1218 AVOutputFormat mpeg1system_muxer = {
1220 "MPEG1 System format",
1223 sizeof(MpegMuxContext),
1225 CODEC_ID_MPEG1VIDEO,
1227 mpeg_mux_write_packet,
1231 #ifdef CONFIG_MPEG1VCD_MUXER
1232 AVOutputFormat mpeg1vcd_muxer = {
1234 "MPEG1 System format (VCD)",
1237 sizeof(MpegMuxContext),
1239 CODEC_ID_MPEG1VIDEO,
1241 mpeg_mux_write_packet,
1245 #ifdef CONFIG_MPEG2VOB_MUXER
1246 AVOutputFormat mpeg2vob_muxer = {
1248 "MPEG2 PS format (VOB)",
1251 sizeof(MpegMuxContext),
1253 CODEC_ID_MPEG2VIDEO,
1255 mpeg_mux_write_packet,
1260 /* Same as mpeg2vob_mux except that the pack size is 2324 */
1261 #ifdef CONFIG_MPEG2SVCD_MUXER
1262 AVOutputFormat mpeg2svcd_muxer = {
1264 "MPEG2 PS format (VOB)",
1267 sizeof(MpegMuxContext),
1269 CODEC_ID_MPEG2VIDEO,
1271 mpeg_mux_write_packet,
1276 /* Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
1277 #ifdef CONFIG_MPEG2DVD_MUXER
1278 AVOutputFormat mpeg2dvd_muxer = {
1280 "MPEG2 PS format (DVD VOB)",
1283 sizeof(MpegMuxContext),
1285 CODEC_ID_MPEG2VIDEO,
1287 mpeg_mux_write_packet,