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 "bitstream.h"
27 #define MAX_PAYLOAD_SIZE 4096
33 typedef struct PacketDesc {
39 struct PacketDesc *next;
45 int max_buffer_size; /* in bytes */
47 PacketDesc *predecode_packet;
48 PacketDesc *premux_packet;
49 PacketDesc **next_packet;
51 uint8_t lpcm_header[3];
55 int64_t vobu_start_pts;
59 int packet_size; /* required packet size */
61 int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
62 int system_header_freq;
63 int system_header_size;
64 int mux_rate; /* bitrate in units of 50 bytes/s */
72 int64_t last_scr; /* current system clock */
74 double vcd_padding_bitrate; //FIXME floats
75 int64_t vcd_padding_bytes_written;
79 AVOutputFormat mpeg1system_muxer;
80 AVOutputFormat mpeg1vcd_muxer;
81 AVOutputFormat mpeg2vob_muxer;
82 AVOutputFormat mpeg2svcd_muxer;
83 AVOutputFormat mpeg2dvd_muxer;
85 static int put_pack_header(AVFormatContext *ctx,
86 uint8_t *buf, int64_t timestamp)
88 MpegMuxContext *s = ctx->priv_data;
91 init_put_bits(&pb, buf, 128);
93 put_bits(&pb, 32, PACK_START_CODE);
95 put_bits(&pb, 2, 0x1);
97 put_bits(&pb, 4, 0x2);
99 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
101 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
103 put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
106 /* clock extension */
110 put_bits(&pb, 22, s->mux_rate);
114 put_bits(&pb, 5, 0x1f); /* reserved */
115 put_bits(&pb, 3, 0); /* stuffing length */
118 return pbBufPtr(&pb) - pb.buf;
121 static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id)
123 MpegMuxContext *s = ctx->priv_data;
124 int size, i, private_stream_coded, id;
127 init_put_bits(&pb, buf, 128);
129 put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
130 put_bits(&pb, 16, 0);
133 put_bits(&pb, 22, s->mux_rate); /* maximum bit rate of the multiplexed stream */
134 put_bits(&pb, 1, 1); /* marker */
135 if (s->is_vcd && only_for_stream_id==VIDEO_ID) {
136 /* This header applies only to the video stream (see VCD standard p. IV-7)*/
139 put_bits(&pb, 6, s->audio_bound);
142 /* see VCD standard, p. IV-7*/
146 put_bits(&pb, 1, 0); /* variable bitrate*/
147 put_bits(&pb, 1, 0); /* non constrainted bit stream */
150 if (s->is_vcd || s->is_dvd) {
151 /* see VCD standard p IV-7 */
152 put_bits(&pb, 1, 1); /* audio locked */
153 put_bits(&pb, 1, 1); /* video locked */
155 put_bits(&pb, 1, 0); /* audio locked */
156 put_bits(&pb, 1, 0); /* video locked */
159 put_bits(&pb, 1, 1); /* marker */
161 if (s->is_vcd && only_for_stream_id==AUDIO_ID) {
162 /* This header applies only to the audio stream (see VCD standard p. IV-7)*/
165 put_bits(&pb, 5, s->video_bound);
168 put_bits(&pb, 1, 0); /* packet_rate_restriction_flag */
169 put_bits(&pb, 7, 0x7f); /* reserved byte */
171 put_bits(&pb, 8, 0xff); /* reserved byte */
173 /* DVD-Video Stream_bound entries
174 id (0xB9) video, maximum P-STD for stream 0xE0. (P-STD_buffer_bound_scale = 1)
175 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)
176 id (0xBD) private stream 1 (audio other than MPEG and subpictures). (P-STD_buffer_bound_scale = 1)
177 id (0xBF) private stream 2, NAV packs, set to 2x1024. */
180 int P_STD_max_video = 0;
181 int P_STD_max_mpeg_audio = 0;
182 int P_STD_max_mpeg_PS1 = 0;
184 for(i=0;i<ctx->nb_streams;i++) {
185 StreamInfo *stream = ctx->streams[i]->priv_data;
188 if (id == 0xbd && stream->max_buffer_size > P_STD_max_mpeg_PS1) {
189 P_STD_max_mpeg_PS1 = stream->max_buffer_size;
190 } else if (id >= 0xc0 && id <= 0xc7 && stream->max_buffer_size > P_STD_max_mpeg_audio) {
191 P_STD_max_mpeg_audio = stream->max_buffer_size;
192 } else if (id == 0xe0 && stream->max_buffer_size > P_STD_max_video) {
193 P_STD_max_video = stream->max_buffer_size;
198 put_bits(&pb, 8, 0xb9); /* stream ID */
201 put_bits(&pb, 13, P_STD_max_video / 1024);
204 if (P_STD_max_mpeg_audio == 0)
205 P_STD_max_mpeg_audio = 4096;
206 put_bits(&pb, 8, 0xb8); /* stream ID */
209 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
211 /* private stream 1 */
212 put_bits(&pb, 8, 0xbd); /* stream ID */
215 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
217 /* private stream 2 */
218 put_bits(&pb, 8, 0xbf); /* stream ID */
221 put_bits(&pb, 13, 2);
224 /* audio stream info */
225 private_stream_coded = 0;
226 for(i=0;i<ctx->nb_streams;i++) {
227 StreamInfo *stream = ctx->streams[i]->priv_data;
230 /* For VCDs, only include the stream info for the stream
231 that the pack which contains this system belongs to.
232 (see VCD standard p. IV-7) */
233 if ( !s->is_vcd || stream->id==only_for_stream_id
234 || only_for_stream_id==0) {
238 /* special case for private streams (AC3 use that) */
239 if (private_stream_coded)
241 private_stream_coded = 1;
244 put_bits(&pb, 8, id); /* stream ID */
249 put_bits(&pb, 13, stream->max_buffer_size / 128);
253 put_bits(&pb, 13, stream->max_buffer_size / 1024);
260 size = pbBufPtr(&pb) - pb.buf;
261 /* patch packet size */
262 buf[4] = (size - 6) >> 8;
263 buf[5] = (size - 6) & 0xff;
268 static int get_system_header_size(AVFormatContext *ctx)
270 int buf_index, i, private_stream_coded;
272 MpegMuxContext *s = ctx->priv_data;
275 return 18; // DVD-Video system headers are 18 bytes fixed length.
278 private_stream_coded = 0;
279 for(i=0;i<ctx->nb_streams;i++) {
280 stream = ctx->streams[i]->priv_data;
281 if (stream->id < 0xc0) {
282 if (private_stream_coded)
284 private_stream_coded = 1;
291 static int mpeg_mux_init(AVFormatContext *ctx)
293 MpegMuxContext *s = ctx->priv_data;
294 int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j;
300 s->packet_number = 0;
301 s->is_vcd = (ctx->oformat == &mpeg1vcd_muxer);
302 s->is_svcd = (ctx->oformat == &mpeg2svcd_muxer);
303 s->is_mpeg2 = (ctx->oformat == &mpeg2vob_muxer || ctx->oformat == &mpeg2svcd_muxer || ctx->oformat == &mpeg2dvd_muxer);
304 s->is_dvd = (ctx->oformat == &mpeg2dvd_muxer);
307 s->packet_size = ctx->packet_size;
309 s->packet_size = 2048;
311 s->vcd_padding_bytes_written = 0;
312 s->vcd_padding_bitrate=0;
322 for(i=0;i<ctx->nb_streams;i++) {
323 st = ctx->streams[i];
324 stream = av_mallocz(sizeof(StreamInfo));
327 st->priv_data = stream;
329 av_set_pts_info(st, 64, 1, 90000);
331 switch(st->codec->codec_type) {
332 case CODEC_TYPE_AUDIO:
333 if (st->codec->codec_id == CODEC_ID_AC3) {
334 stream->id = ac3_id++;
335 } else if (st->codec->codec_id == CODEC_ID_DTS) {
336 stream->id = dts_id++;
337 } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
338 stream->id = lpcm_id++;
339 for(j = 0; j < 4; j++) {
340 if (lpcm_freq_tab[j] == st->codec->sample_rate)
345 if (st->codec->channels > 8)
347 stream->lpcm_header[0] = 0x0c;
348 stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4);
349 stream->lpcm_header[2] = 0x80;
350 stream->lpcm_align = st->codec->channels * 2;
352 stream->id = mpa_id++;
355 /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
356 Right now it is also used for everything else.*/
357 stream->max_buffer_size = 4 * 1024;
360 case CODEC_TYPE_VIDEO:
361 stream->id = mpv_id++;
362 if (st->codec->rc_buffer_size)
363 stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8;
365 stream->max_buffer_size = 230*1024; //FIXME this is probably too small as default
367 /* see VCD standard, p. IV-7*/
368 stream->max_buffer_size = 46 * 1024;
370 /* This value HAS to be used for SVCD (see SVCD standard, p. 26 V.2.3.2).
371 Right now it is also used for everything else.*/
372 stream->max_buffer_size = 230 * 1024;
376 case CODEC_TYPE_SUBTITLE:
377 stream->id = mps_id++;
378 stream->max_buffer_size = 16 * 1024;
383 av_fifo_init(&stream->fifo, 16);
388 for(i=0;i<ctx->nb_streams;i++) {
390 st = ctx->streams[i];
391 stream = (StreamInfo*) st->priv_data;
393 if(st->codec->rc_max_rate || stream->id==VIDEO_ID)
394 codec_rate= st->codec->rc_max_rate;
396 codec_rate= st->codec->bit_rate;
399 codec_rate= (1<<21)*8*50/ctx->nb_streams;
401 bitrate += codec_rate;
403 if (stream->id==AUDIO_ID)
404 audio_bitrate += codec_rate;
405 else if (stream->id==VIDEO_ID)
406 video_bitrate += codec_rate;
410 s->mux_rate= (ctx->mux_rate + (8 * 50) - 1) / (8 * 50);
412 /* we increase slightly the bitrate to take into account the
413 headers. XXX: compute it exactly */
414 bitrate += bitrate*5/100;
416 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
420 double overhead_rate;
422 /* The VCD standard mandates that the mux_rate field is 3528
423 (see standard p. IV-6).
424 The value is actually "wrong", i.e. if you calculate
425 it using the normal formula and the 75 sectors per second transfer
426 rate you get a different value because the real pack size is 2324,
427 not 2352. But the standard explicitly specifies that the mux_rate
428 field in the header must have this value.*/
429 // s->mux_rate=2352 * 75 / 50; /* = 3528*/
431 /* The VCD standard states that the muxed stream must be
432 exactly 75 packs / second (the data rate of a single speed cdrom).
433 Since the video bitrate (probably 1150000 bits/sec) will be below
434 the theoretical maximum we have to add some padding packets
435 to make up for the lower data rate.
436 (cf. VCD standard p. IV-6 )*/
438 /* Add the header overhead to the data rate.
439 2279 data bytes per audio pack, 2294 data bytes per video pack*/
440 overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
441 overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
444 /* Add padding so that the full bitrate is 2324*75 bytes/sec */
445 s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate);
448 if (s->is_vcd || s->is_mpeg2)
450 s->pack_header_freq = 1;
452 /* every 2 seconds */
453 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
455 /* the above seems to make pack_header_freq zero sometimes */
456 if (s->pack_header_freq == 0)
457 s->pack_header_freq = 1;
460 /* every 200 packets. Need to look at the spec. */
461 s->system_header_freq = s->pack_header_freq * 40;
463 /* the standard mandates that there are only two system headers
464 in the whole file: one in the first packet of each stream.
465 (see standard p. IV-7 and IV-8) */
466 s->system_header_freq = 0x7fffffff;
468 s->system_header_freq = s->pack_header_freq * 5;
470 for(i=0;i<ctx->nb_streams;i++) {
471 stream = ctx->streams[i]->priv_data;
472 stream->packet_number = 0;
474 s->system_header_size = get_system_header_size(ctx);
478 for(i=0;i<ctx->nb_streams;i++) {
479 av_free(ctx->streams[i]->priv_data);
481 return AVERROR(ENOMEM);
484 static inline void put_timestamp(ByteIOContext *pb, int id, int64_t timestamp)
488 (((timestamp >> 30) & 0x07) << 1) |
490 put_be16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
491 put_be16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
495 /* return the number of padding bytes that should be inserted into
496 the multiplexed stream.*/
497 static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
499 MpegMuxContext *s = ctx->priv_data;
502 if (s->vcd_padding_bitrate > 0 && pts!=AV_NOPTS_VALUE)
504 int64_t full_pad_bytes;
506 full_pad_bytes = (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0); //FIXME this is wrong
507 pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written);
510 /* might happen if we have already padded to a later timestamp. This
511 can occur if another stream has already advanced further.*/
519 #if 0 /* unused, remove? */
520 /* return the exact available payload size for the next packet for
521 stream 'stream_index'. 'pts' and 'dts' are only used to know if
522 timestamps are needed in the packet header. */
523 static int get_packet_payload_size(AVFormatContext *ctx, int stream_index,
524 int64_t pts, int64_t dts)
526 MpegMuxContext *s = ctx->priv_data;
530 stream = ctx->streams[stream_index]->priv_data;
533 if (((s->packet_number % s->pack_header_freq) == 0)) {
534 /* pack header size */
541 /* there is exactly one system header for each stream in a VCD MPEG,
542 One in the very first video packet and one in the very first
543 audio packet (see VCD standard p. IV-7 and IV-8).*/
545 if (stream->packet_number==0)
546 /* The system headers refer only to the stream they occur in,
547 so they have a constant size.*/
551 if ((s->packet_number % s->system_header_freq) == 0)
552 buf_index += s->system_header_size;
556 if ((s->is_vcd && stream->packet_number==0)
557 || (s->is_svcd && s->packet_number==0))
558 /* the first pack of each stream contains only the pack header,
559 the system header and some padding (see VCD standard p. IV-6)
560 Add the padding size, so that the actual payload becomes 0.*/
561 buf_index += s->packet_size - buf_index;
563 /* packet header size */
567 if (stream->packet_number==0)
568 buf_index += 3; /* PES extension */
569 buf_index += 1; /* obligatory stuffing byte */
571 if (pts != AV_NOPTS_VALUE) {
582 if (stream->id < 0xc0) {
583 /* AC3/LPCM private data header */
585 if (stream->id >= 0xa0) {
588 /* NOTE: we round the payload size to an integer number of
590 n = (s->packet_size - buf_index) % stream->lpcm_align;
592 buf_index += (stream->lpcm_align - n);
596 if (s->is_vcd && stream->id == AUDIO_ID)
597 /* The VCD standard demands that 20 zero bytes follow
598 each audio packet (see standard p. IV-8).*/
601 return s->packet_size - buf_index;
605 /* Write an MPEG padding packet header. */
606 static void put_padding_packet(AVFormatContext *ctx, ByteIOContext *pb,int packet_bytes)
608 MpegMuxContext *s = ctx->priv_data;
611 put_be32(pb, PADDING_STREAM);
612 put_be16(pb, packet_bytes - 6);
619 for(i=0;i<packet_bytes;i++)
623 static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len){
625 PacketDesc *pkt_desc= stream->premux_packet;
628 if(pkt_desc->size == pkt_desc->unwritten_size)
630 len -= pkt_desc->unwritten_size;
631 pkt_desc= pkt_desc->next;
637 /* flush the packet on stream stream_index */
638 static int flush_packet(AVFormatContext *ctx, int stream_index,
639 int64_t pts, int64_t dts, int64_t scr, int trailer_size)
641 MpegMuxContext *s = ctx->priv_data;
642 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
644 int size, payload_size, startcode, id, stuffing_size, i, header_len;
647 int zero_trail_bytes = 0;
648 int pad_packet_bytes = 0;
650 int general_pack = 0; /*"general" pack without data specific to one stream?*/
656 printf("packet ID=%2x PTS=%0.3f\n",
662 if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) {
663 /* output pack and systems header if needed */
664 size = put_pack_header(ctx, buf_ptr, scr);
669 /* there is exactly one system header for each stream in a VCD MPEG,
670 One in the very first video packet and one in the very first
671 audio packet (see VCD standard p. IV-7 and IV-8).*/
673 if (stream->packet_number==0) {
674 size = put_system_header(ctx, buf_ptr, id);
677 } else if (s->is_dvd) {
678 if (stream->align_iframe || s->packet_number == 0){
679 int PES_bytes_to_fill = s->packet_size - size - 10;
681 if (pts != AV_NOPTS_VALUE) {
683 PES_bytes_to_fill -= 5 + 5;
685 PES_bytes_to_fill -= 5;
688 if (stream->bytes_to_iframe == 0 || s->packet_number == 0) {
689 size = put_system_header(ctx, buf_ptr, 0);
691 size = buf_ptr - buffer;
692 put_buffer(&ctx->pb, buffer, size);
694 put_be32(&ctx->pb, PRIVATE_STREAM_2);
695 put_be16(&ctx->pb, 0x03d4); // length
696 put_byte(&ctx->pb, 0x00); // substream ID, 00=PCI
697 for (i = 0; i < 979; i++)
698 put_byte(&ctx->pb, 0x00);
700 put_be32(&ctx->pb, PRIVATE_STREAM_2);
701 put_be16(&ctx->pb, 0x03fa); // length
702 put_byte(&ctx->pb, 0x01); // substream ID, 01=DSI
703 for (i = 0; i < 1017; i++)
704 put_byte(&ctx->pb, 0x00);
706 memset(buffer, 0, 128);
709 stream->align_iframe = 0;
710 scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
711 size = put_pack_header(ctx, buf_ptr, scr);
715 } else if (stream->bytes_to_iframe < PES_bytes_to_fill) {
716 pad_packet_bytes = PES_bytes_to_fill - stream->bytes_to_iframe;
720 if ((s->packet_number % s->system_header_freq) == 0) {
721 size = put_system_header(ctx, buf_ptr, 0);
726 size = buf_ptr - buffer;
727 put_buffer(&ctx->pb, buffer, size);
729 packet_size = s->packet_size - size;
731 if (s->is_vcd && id == AUDIO_ID)
732 /* The VCD standard demands that 20 zero bytes follow
733 each audio pack (see standard p. IV-8).*/
734 zero_trail_bytes += 20;
736 if ((s->is_vcd && stream->packet_number==0)
737 || (s->is_svcd && s->packet_number==0)) {
738 /* for VCD the first pack of each stream contains only the pack header,
739 the system header and lots of padding (see VCD standard p. IV-6).
740 In the case of an audio pack, 20 zero bytes are also added at
742 /* For SVCD we fill the very first pack to increase compatibility with
743 some DVD players. Not mandated by the standard.*/
745 general_pack = 1; /* the system header refers to both streams and no stream data*/
746 pad_packet_bytes = packet_size - zero_trail_bytes;
749 packet_size -= pad_packet_bytes + zero_trail_bytes;
751 if (packet_size > 0) {
753 /* packet header size */
759 if (stream->packet_number==0)
760 header_len += 3; /* PES extension */
761 header_len += 1; /* obligatory stuffing byte */
765 if (pts != AV_NOPTS_VALUE) {
775 payload_size = packet_size - header_len;
777 startcode = PRIVATE_STREAM_1;
785 startcode = 0x100 + id;
788 stuffing_size = payload_size - av_fifo_size(&stream->fifo);
790 // first byte does not fit -> reset pts/dts + stuffing
791 if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){
795 if(pts != AV_NOPTS_VALUE)
796 timestamp_len += s->is_mpeg2 ? 5 : 4;
797 pts=dts= AV_NOPTS_VALUE;
798 header_len -= timestamp_len;
799 if (s->is_dvd && stream->align_iframe) {
800 pad_packet_bytes += timestamp_len;
801 packet_size -= timestamp_len;
803 payload_size += timestamp_len;
805 stuffing_size += timestamp_len;
806 if(payload_size > trailer_size)
807 stuffing_size += payload_size - trailer_size;
810 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) { // can't use padding, so use stuffing
811 packet_size += pad_packet_bytes;
812 payload_size += pad_packet_bytes; // undo the previous adjustment
813 if (stuffing_size < 0) {
814 stuffing_size = pad_packet_bytes;
816 stuffing_size += pad_packet_bytes;
818 pad_packet_bytes = 0;
821 if (stuffing_size < 0)
823 if (stuffing_size > 16) { /*<=16 for MPEG-1, <=32 for MPEG-2*/
824 pad_packet_bytes += stuffing_size;
825 packet_size -= stuffing_size;
826 payload_size -= stuffing_size;
830 nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size);
832 put_be32(&ctx->pb, startcode);
834 put_be16(&ctx->pb, packet_size);
837 for(i=0;i<stuffing_size;i++)
838 put_byte(&ctx->pb, 0xff);
841 put_byte(&ctx->pb, 0x80); /* mpeg2 id */
845 if (pts != AV_NOPTS_VALUE) {
851 /* Both the MPEG-2 and the SVCD standards demand that the
852 P-STD_buffer_size field be included in the first packet of
853 every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
854 and MPEG-2 standard 2.7.7) */
855 if (stream->packet_number == 0)
858 put_byte(&ctx->pb, pes_flags); /* flags */
859 put_byte(&ctx->pb, header_len - 3 + stuffing_size);
861 if (pes_flags & 0x80) /*write pts*/
862 put_timestamp(&ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
863 if (pes_flags & 0x40) /*write dts*/
864 put_timestamp(&ctx->pb, 0x01, dts);
866 if (pes_flags & 0x01) { /*write pes extension*/
867 put_byte(&ctx->pb, 0x10); /* flags */
869 /* P-STD buffer info */
871 put_be16(&ctx->pb, 0x4000 | stream->max_buffer_size/128);
873 put_be16(&ctx->pb, 0x6000 | stream->max_buffer_size/1024);
877 if (pts != AV_NOPTS_VALUE) {
879 put_timestamp(&ctx->pb, 0x03, pts);
880 put_timestamp(&ctx->pb, 0x01, dts);
882 put_timestamp(&ctx->pb, 0x02, pts);
885 put_byte(&ctx->pb, 0x0f);
890 /* special stuffing byte that is always written
891 to prevent accidental generation of start codes. */
892 put_byte(&ctx->pb, 0xff);
894 for(i=0;i<stuffing_size;i++)
895 put_byte(&ctx->pb, 0xff);
898 if (startcode == PRIVATE_STREAM_1) {
899 put_byte(&ctx->pb, id);
901 /* LPCM (XXX: check nb_frames) */
902 put_byte(&ctx->pb, 7);
903 put_be16(&ctx->pb, 4); /* skip 3 header bytes */
904 put_byte(&ctx->pb, stream->lpcm_header[0]);
905 put_byte(&ctx->pb, stream->lpcm_header[1]);
906 put_byte(&ctx->pb, stream->lpcm_header[2]);
907 } else if (id >= 0x40) {
909 put_byte(&ctx->pb, nb_frames);
910 put_be16(&ctx->pb, trailer_size+1);
915 if(av_fifo_generic_read(&stream->fifo, payload_size - stuffing_size, &put_buffer, &ctx->pb) < 0)
917 stream->bytes_to_iframe -= payload_size - stuffing_size;
923 if (pad_packet_bytes > 0)
924 put_padding_packet(ctx,&ctx->pb, pad_packet_bytes);
926 for(i=0;i<zero_trail_bytes;i++)
927 put_byte(&ctx->pb, 0x00);
929 put_flush_packet(&ctx->pb);
933 /* only increase the stream packet number if this pack actually contains
934 something that is specific to this stream! I.e. a dedicated header
937 stream->packet_number++;
939 return payload_size - stuffing_size;
942 static void put_vcd_padding_sector(AVFormatContext *ctx)
944 /* There are two ways to do this padding: writing a sector/pack
945 of 0 values, or writing an MPEG padding pack. Both seem to
946 work with most decoders, BUT the VCD standard only allows a 0-sector
947 (see standard p. IV-4, IV-5).
948 So a 0-sector it is...*/
950 MpegMuxContext *s = ctx->priv_data;
953 for(i=0;i<s->packet_size;i++)
954 put_byte(&ctx->pb, 0);
956 s->vcd_padding_bytes_written += s->packet_size;
958 put_flush_packet(&ctx->pb);
960 /* increasing the packet number is correct. The SCR of the following packs
961 is calculated from the packet_number and it has to include the padding
962 sector (it represents the sector index, not the MPEG pack index)
963 (see VCD standard p. IV-6)*/
967 #if 0 /* unused, remove? */
968 static int64_t get_vcd_scr(AVFormatContext *ctx,int stream_index,int64_t pts)
970 MpegMuxContext *s = ctx->priv_data;
973 /* Since the data delivery rate is constant, SCR is computed
974 using the formula C + i * 1200 where C is the start constant
975 and i is the pack index.
976 It is recommended that SCR 0 is at the beginning of the VCD front
977 margin (a sequence of empty Form 2 sectors on the CD).
978 It is recommended that the front margin is 30 sectors long, so
979 we use C = 30*1200 = 36000
980 (Note that even if the front margin is not 30 sectors the file
981 will still be correct according to the standard. It just won't have
982 the "recommended" value).*/
983 scr = 36000 + s->packet_number * 1200;
989 static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){
990 // MpegMuxContext *s = ctx->priv_data;
993 for(i=0; i<ctx->nb_streams; i++){
994 AVStream *st = ctx->streams[i];
995 StreamInfo *stream = st->priv_data;
996 PacketDesc *pkt_desc;
998 while((pkt_desc= stream->predecode_packet)
999 && scr > pkt_desc->dts){ //FIXME > vs >=
1000 if(stream->buffer_index < pkt_desc->size ||
1001 stream->predecode_packet == stream->premux_packet){
1002 av_log(ctx, AV_LOG_ERROR,
1003 "buffer underflow i=%d bufi=%d size=%d\n",
1004 i, stream->buffer_index, pkt_desc->size);
1007 stream->buffer_index -= pkt_desc->size;
1009 stream->predecode_packet= pkt_desc->next;
1010 av_freep(&pkt_desc);
1017 static int output_packet(AVFormatContext *ctx, int flush){
1018 MpegMuxContext *s = ctx->priv_data;
1021 int i, avail_space, es_size, trailer_size;
1023 int best_score= INT_MIN;
1024 int ignore_constraints=0;
1025 int64_t scr= s->last_scr;
1026 PacketDesc *timestamp_packet;
1027 const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
1030 for(i=0; i<ctx->nb_streams; i++){
1031 AVStream *st = ctx->streams[i];
1032 StreamInfo *stream = st->priv_data;
1033 const int avail_data= av_fifo_size(&stream->fifo);
1034 const int space= stream->max_buffer_size - stream->buffer_index;
1035 int rel_space= 1024*space / stream->max_buffer_size;
1036 PacketDesc *next_pkt= stream->premux_packet;
1038 /* for subtitle, a single PES packet must be generated,
1039 so we flush after every single subtitle packet */
1040 if(s->packet_size > avail_data && !flush
1041 && st->codec->codec_type != CODEC_TYPE_SUBTITLE)
1045 assert(avail_data>0);
1047 if(space < s->packet_size && !ignore_constraints)
1050 if(next_pkt && next_pkt->dts - scr > max_delay)
1053 if(rel_space > best_score){
1054 best_score= rel_space;
1061 int64_t best_dts= INT64_MAX;
1063 for(i=0; i<ctx->nb_streams; i++){
1064 AVStream *st = ctx->streams[i];
1065 StreamInfo *stream = st->priv_data;
1066 PacketDesc *pkt_desc= stream->predecode_packet;
1067 if(pkt_desc && pkt_desc->dts < best_dts)
1068 best_dts= pkt_desc->dts;
1072 av_log(ctx, AV_LOG_DEBUG, "bumping scr, scr:%f, dts:%f\n",
1073 scr/90000.0, best_dts/90000.0);
1075 if(best_dts == INT64_MAX)
1078 if(scr >= best_dts+1 && !ignore_constraints){
1079 av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n");
1080 ignore_constraints= 1;
1082 scr= FFMAX(best_dts+1, scr);
1083 if(remove_decoded_packets(ctx, scr) < 0)
1088 assert(best_i >= 0);
1090 st = ctx->streams[best_i];
1091 stream = st->priv_data;
1093 assert(av_fifo_size(&stream->fifo) > 0);
1095 assert(avail_space >= s->packet_size || ignore_constraints);
1097 timestamp_packet= stream->premux_packet;
1098 if(timestamp_packet->unwritten_size == timestamp_packet->size){
1101 trailer_size= timestamp_packet->unwritten_size;
1102 timestamp_packet= timestamp_packet->next;
1105 if(timestamp_packet){
1106 //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);
1107 es_size= flush_packet(ctx, best_i, timestamp_packet->pts, timestamp_packet->dts, scr, trailer_size);
1109 assert(av_fifo_size(&stream->fifo) == trailer_size);
1110 es_size= flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr, trailer_size);
1114 /* Write one or more padding sectors, if necessary, to reach
1115 the constant overall bitrate.*/
1118 while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->premux_packet->pts) ) >= s->packet_size){ //FIXME pts cannot be correct here
1119 put_vcd_padding_sector(ctx);
1120 s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
1124 stream->buffer_index += es_size;
1125 s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
1127 while(stream->premux_packet && stream->premux_packet->unwritten_size <= es_size){
1128 es_size -= stream->premux_packet->unwritten_size;
1129 stream->premux_packet= stream->premux_packet->next;
1132 stream->premux_packet->unwritten_size -= es_size;
1134 if(remove_decoded_packets(ctx, s->last_scr) < 0)
1140 static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
1142 MpegMuxContext *s = ctx->priv_data;
1143 int stream_index= pkt->stream_index;
1144 int size= pkt->size;
1145 uint8_t *buf= pkt->data;
1146 AVStream *st = ctx->streams[stream_index];
1147 StreamInfo *stream = st->priv_data;
1149 PacketDesc *pkt_desc;
1150 const int preload= av_rescale(ctx->preload, 90000, AV_TIME_BASE);
1151 const int is_iframe = st->codec->codec_type == CODEC_TYPE_VIDEO && (pkt->flags & PKT_FLAG_KEY);
1156 if(pts != AV_NOPTS_VALUE) pts += preload;
1157 if(dts != AV_NOPTS_VALUE) dts += preload;
1159 //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);
1160 if (!stream->premux_packet)
1161 stream->next_packet = &stream->premux_packet;
1162 *stream->next_packet=
1163 pkt_desc= av_mallocz(sizeof(PacketDesc));
1166 pkt_desc->unwritten_size=
1167 pkt_desc->size= size;
1168 if(!stream->predecode_packet)
1169 stream->predecode_packet= pkt_desc;
1170 stream->next_packet= &pkt_desc->next;
1172 av_fifo_realloc(&stream->fifo, av_fifo_size(&stream->fifo) + size + 1);
1175 if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) { // min VOBU length 0.4 seconds (mpucoder)
1176 stream->bytes_to_iframe = av_fifo_size(&stream->fifo);
1177 stream->align_iframe = 1;
1178 stream->vobu_start_pts = pts;
1180 stream->align_iframe = 0;
1184 av_fifo_write(&stream->fifo, buf, size);
1187 int ret= output_packet(ctx, 0);
1193 static int mpeg_mux_end(AVFormatContext *ctx)
1195 // MpegMuxContext *s = ctx->priv_data;
1200 int ret= output_packet(ctx, 1);
1207 /* End header according to MPEG1 systems standard. We do not write
1208 it as it is usually not needed by decoders and because it
1209 complicates MPEG stream concatenation. */
1210 //put_be32(&ctx->pb, ISO_11172_END_CODE);
1211 //put_flush_packet(&ctx->pb);
1213 for(i=0;i<ctx->nb_streams;i++) {
1214 stream = ctx->streams[i]->priv_data;
1216 assert(av_fifo_size(&stream->fifo) == 0);
1217 av_fifo_free(&stream->fifo);
1222 #ifdef CONFIG_MPEG1SYSTEM_MUXER
1223 AVOutputFormat mpeg1system_muxer = {
1225 "MPEG1 System format",
1228 sizeof(MpegMuxContext),
1230 CODEC_ID_MPEG1VIDEO,
1232 mpeg_mux_write_packet,
1236 #ifdef CONFIG_MPEG1VCD_MUXER
1237 AVOutputFormat mpeg1vcd_muxer = {
1239 "MPEG1 System format (VCD)",
1242 sizeof(MpegMuxContext),
1244 CODEC_ID_MPEG1VIDEO,
1246 mpeg_mux_write_packet,
1250 #ifdef CONFIG_MPEG2VOB_MUXER
1251 AVOutputFormat mpeg2vob_muxer = {
1253 "MPEG2 PS format (VOB)",
1256 sizeof(MpegMuxContext),
1258 CODEC_ID_MPEG2VIDEO,
1260 mpeg_mux_write_packet,
1265 /* Same as mpeg2vob_mux except that the pack size is 2324 */
1266 #ifdef CONFIG_MPEG2SVCD_MUXER
1267 AVOutputFormat mpeg2svcd_muxer = {
1269 "MPEG2 PS format (VOB)",
1272 sizeof(MpegMuxContext),
1274 CODEC_ID_MPEG2VIDEO,
1276 mpeg_mux_write_packet,
1281 /* Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
1282 #ifdef CONFIG_MPEG2DVD_MUXER
1283 AVOutputFormat mpeg2dvd_muxer = {
1285 "MPEG2 PS format (DVD VOB)",
1288 sizeof(MpegMuxContext),
1290 CODEC_ID_MPEG2VIDEO,
1292 mpeg_mux_write_packet,