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
24 #include "libavutil/attributes.h"
25 #include "libavutil/fifo.h"
26 #include "libavutil/log.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/opt.h"
29 #include "libavcodec/put_bits.h"
34 #define MAX_PAYLOAD_SIZE 4096
39 typedef struct PacketDesc {
45 struct PacketDesc *next;
51 int max_buffer_size; /* in bytes */
53 PacketDesc *predecode_packet;
54 PacketDesc *premux_packet;
55 PacketDesc **next_packet;
57 uint8_t lpcm_header[3];
61 int64_t vobu_start_pts;
66 int packet_size; /* required packet size */
68 int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
69 int system_header_freq;
70 int system_header_size;
71 int user_mux_rate; /* bitrate in units of bits/s */
72 int mux_rate; /* bitrate in units of 50 bytes/s */
80 int64_t last_scr; /* current system clock */
82 double vcd_padding_bitrate; //FIXME floats
83 int64_t vcd_padding_bytes_written;
88 extern AVOutputFormat ff_mpeg1vcd_muxer;
89 extern AVOutputFormat ff_mpeg2dvd_muxer;
90 extern AVOutputFormat ff_mpeg2svcd_muxer;
91 extern AVOutputFormat ff_mpeg2vob_muxer;
93 static int put_pack_header(AVFormatContext *ctx,
94 uint8_t *buf, int64_t timestamp)
96 MpegMuxContext *s = ctx->priv_data;
99 init_put_bits(&pb, buf, 128);
101 put_bits32(&pb, PACK_START_CODE);
103 put_bits(&pb, 2, 0x1);
105 put_bits(&pb, 4, 0x2);
107 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
109 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
111 put_bits(&pb, 15, (uint32_t)((timestamp ) & 0x7fff));
114 /* clock extension */
118 put_bits(&pb, 22, s->mux_rate);
122 put_bits(&pb, 5, 0x1f); /* reserved */
123 put_bits(&pb, 3, 0); /* stuffing length */
126 return put_bits_ptr(&pb) - pb.buf;
129 static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id)
131 MpegMuxContext *s = ctx->priv_data;
132 int size, i, private_stream_coded, id;
135 init_put_bits(&pb, buf, 128);
137 put_bits32(&pb, SYSTEM_HEADER_START_CODE);
138 put_bits(&pb, 16, 0);
141 put_bits(&pb, 22, s->mux_rate); /* maximum bit rate of the multiplexed stream */
142 put_bits(&pb, 1, 1); /* marker */
143 if (s->is_vcd && only_for_stream_id==VIDEO_ID) {
144 /* This header applies only to the video stream (see VCD standard p. IV-7)*/
147 put_bits(&pb, 6, s->audio_bound);
150 /* see VCD standard, p. IV-7*/
154 put_bits(&pb, 1, 0); /* variable bitrate*/
155 put_bits(&pb, 1, 0); /* non constrainted bit stream */
158 if (s->is_vcd || s->is_dvd) {
159 /* see VCD standard p IV-7 */
160 put_bits(&pb, 1, 1); /* audio locked */
161 put_bits(&pb, 1, 1); /* video locked */
163 put_bits(&pb, 1, 0); /* audio locked */
164 put_bits(&pb, 1, 0); /* video locked */
167 put_bits(&pb, 1, 1); /* marker */
169 if (s->is_vcd && (only_for_stream_id & 0xe0) == AUDIO_ID) {
170 /* This header applies only to the audio stream (see VCD standard p. IV-7)*/
173 put_bits(&pb, 5, s->video_bound);
176 put_bits(&pb, 1, 0); /* packet_rate_restriction_flag */
177 put_bits(&pb, 7, 0x7f); /* reserved byte */
179 put_bits(&pb, 8, 0xff); /* reserved byte */
181 /* DVD-Video Stream_bound entries
182 id (0xB9) video, maximum P-STD for stream 0xE0. (P-STD_buffer_bound_scale = 1)
183 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)
184 id (0xBD) private stream 1 (audio other than MPEG and subpictures). (P-STD_buffer_bound_scale = 1)
185 id (0xBF) private stream 2, NAV packs, set to 2x1024. */
188 int P_STD_max_video = 0;
189 int P_STD_max_mpeg_audio = 0;
190 int P_STD_max_mpeg_PS1 = 0;
192 for(i=0;i<ctx->nb_streams;i++) {
193 StreamInfo *stream = ctx->streams[i]->priv_data;
196 if (id == 0xbd && stream->max_buffer_size > P_STD_max_mpeg_PS1) {
197 P_STD_max_mpeg_PS1 = stream->max_buffer_size;
198 } else if (id >= 0xc0 && id <= 0xc7 && stream->max_buffer_size > P_STD_max_mpeg_audio) {
199 P_STD_max_mpeg_audio = stream->max_buffer_size;
200 } else if (id == 0xe0 && stream->max_buffer_size > P_STD_max_video) {
201 P_STD_max_video = stream->max_buffer_size;
206 put_bits(&pb, 8, 0xb9); /* stream ID */
209 put_bits(&pb, 13, P_STD_max_video / 1024);
212 if (P_STD_max_mpeg_audio == 0)
213 P_STD_max_mpeg_audio = 4096;
214 put_bits(&pb, 8, 0xb8); /* stream ID */
217 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
219 /* private stream 1 */
220 put_bits(&pb, 8, 0xbd); /* stream ID */
223 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
225 /* private stream 2 */
226 put_bits(&pb, 8, 0xbf); /* stream ID */
229 put_bits(&pb, 13, 2);
232 /* audio stream info */
233 private_stream_coded = 0;
234 for(i=0;i<ctx->nb_streams;i++) {
235 StreamInfo *stream = ctx->streams[i]->priv_data;
238 /* For VCDs, only include the stream info for the stream
239 that the pack which contains this system belongs to.
240 (see VCD standard p. IV-7) */
241 if ( !s->is_vcd || stream->id==only_for_stream_id
242 || only_for_stream_id==0) {
246 /* special case for private streams (AC-3 uses that) */
247 if (private_stream_coded)
249 private_stream_coded = 1;
252 put_bits(&pb, 8, id); /* stream ID */
257 put_bits(&pb, 13, stream->max_buffer_size / 128);
261 put_bits(&pb, 13, stream->max_buffer_size / 1024);
268 size = put_bits_ptr(&pb) - pb.buf;
269 /* patch packet size */
270 AV_WB16(buf + 4, size - 6);
275 static int get_system_header_size(AVFormatContext *ctx)
277 int buf_index, i, private_stream_coded;
279 MpegMuxContext *s = ctx->priv_data;
282 return 18; // DVD-Video system headers are 18 bytes fixed length.
285 private_stream_coded = 0;
286 for(i=0;i<ctx->nb_streams;i++) {
287 stream = ctx->streams[i]->priv_data;
288 if (stream->id < 0xc0) {
289 if (private_stream_coded)
291 private_stream_coded = 1;
298 static av_cold int mpeg_mux_init(AVFormatContext *ctx)
300 MpegMuxContext *s = ctx->priv_data;
301 int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j;
307 s->packet_number = 0;
308 s->is_vcd = (CONFIG_MPEG1VCD_MUXER && ctx->oformat == &ff_mpeg1vcd_muxer);
309 s->is_svcd = (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer);
310 s->is_mpeg2 = ((CONFIG_MPEG2VOB_MUXER && ctx->oformat == &ff_mpeg2vob_muxer) ||
311 (CONFIG_MPEG2DVD_MUXER && ctx->oformat == &ff_mpeg2dvd_muxer) ||
312 (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer));
313 s->is_dvd = (CONFIG_MPEG2DVD_MUXER && ctx->oformat == &ff_mpeg2dvd_muxer);
315 if(ctx->packet_size) {
316 if (ctx->packet_size < 20 || ctx->packet_size > (1 << 23) + 10) {
317 av_log(ctx, AV_LOG_ERROR, "Invalid packet size %d\n",
321 s->packet_size = ctx->packet_size;
323 s->packet_size = 2048;
324 if (ctx->max_delay < 0) /* Not set by the caller */
325 ctx->max_delay = 0.7*AV_TIME_BASE;
327 s->vcd_padding_bytes_written = 0;
328 s->vcd_padding_bitrate=0;
338 for(i=0;i<ctx->nb_streams;i++) {
339 st = ctx->streams[i];
340 stream = av_mallocz(sizeof(StreamInfo));
343 st->priv_data = stream;
345 avpriv_set_pts_info(st, 64, 1, 90000);
347 switch(st->codec->codec_type) {
348 case AVMEDIA_TYPE_AUDIO:
350 (st->codec->codec_id == AV_CODEC_ID_AC3 ||
351 st->codec->codec_id == AV_CODEC_ID_DTS ||
352 st->codec->codec_id == AV_CODEC_ID_PCM_S16BE))
353 av_log(ctx, AV_LOG_WARNING,
354 "%s in MPEG-1 system streams is not widely supported, "
355 "consider using the vob or the dvd muxer "
356 "to force a MPEG-2 program stream.\n",
357 avcodec_get_name(st->codec->codec_id));
358 if (st->codec->codec_id == AV_CODEC_ID_AC3) {
359 stream->id = ac3_id++;
360 } else if (st->codec->codec_id == AV_CODEC_ID_DTS) {
361 stream->id = dts_id++;
362 } else if (st->codec->codec_id == AV_CODEC_ID_PCM_S16BE) {
363 stream->id = lpcm_id++;
364 for(j = 0; j < 4; j++) {
365 if (lpcm_freq_tab[j] == st->codec->sample_rate)
370 if (st->codec->channels > 8)
372 stream->lpcm_header[0] = 0x0c;
373 stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4);
374 stream->lpcm_header[2] = 0x80;
375 stream->lpcm_align = st->codec->channels * 2;
377 stream->id = mpa_id++;
380 /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
381 Right now it is also used for everything else.*/
382 stream->max_buffer_size = 4 * 1024;
385 case AVMEDIA_TYPE_VIDEO:
386 stream->id = mpv_id++;
387 if (st->codec->rc_buffer_size)
388 stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8;
390 av_log(ctx, AV_LOG_WARNING, "VBV buffer size not set, using default size of 130KB\n"
391 "If you want the mpeg file to be compliant to some specification\n"
392 "Like DVD, VCD or others, make sure you set the correct buffer size\n");
393 stream->max_buffer_size = 230*1024; //FIXME this is probably too small as default
395 if (stream->max_buffer_size > 1024 * 8191) {
396 av_log(ctx, AV_LOG_WARNING, "buffer size %d, too large\n", stream->max_buffer_size);
397 stream->max_buffer_size = 1024 * 8191;
401 case AVMEDIA_TYPE_SUBTITLE:
402 stream->id = mps_id++;
403 stream->max_buffer_size = 16 * 1024;
408 stream->fifo= av_fifo_alloc(16);
415 for(i=0;i<ctx->nb_streams;i++) {
417 st = ctx->streams[i];
418 stream = (StreamInfo*) st->priv_data;
420 if(st->codec->rc_max_rate || stream->id==VIDEO_ID)
421 codec_rate= st->codec->rc_max_rate;
423 codec_rate= st->codec->bit_rate;
426 codec_rate= (1<<21)*8*50/ctx->nb_streams;
428 bitrate += codec_rate;
430 if ((stream->id & 0xe0) == AUDIO_ID)
431 audio_bitrate += codec_rate;
432 else if (stream->id==VIDEO_ID)
433 video_bitrate += codec_rate;
436 if (s->user_mux_rate) {
437 s->mux_rate = (s->user_mux_rate + (8 * 50) - 1) / (8 * 50);
439 /* we increase slightly the bitrate to take into account the
440 headers. XXX: compute it exactly */
441 bitrate += bitrate / 20;
443 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
444 if (s->mux_rate >= (1<<22)) {
445 av_log(ctx, AV_LOG_WARNING, "mux rate %d is too large\n", s->mux_rate);
446 s->mux_rate = (1<<22) - 1;
451 double overhead_rate;
453 /* The VCD standard mandates that the mux_rate field is 3528
454 (see standard p. IV-6).
455 The value is actually "wrong", i.e. if you calculate
456 it using the normal formula and the 75 sectors per second transfer
457 rate you get a different value because the real pack size is 2324,
458 not 2352. But the standard explicitly specifies that the mux_rate
459 field in the header must have this value.*/
460 // s->mux_rate=2352 * 75 / 50; /* = 3528*/
462 /* The VCD standard states that the muxed stream must be
463 exactly 75 packs / second (the data rate of a single speed cdrom).
464 Since the video bitrate (probably 1150000 bits/sec) will be below
465 the theoretical maximum we have to add some padding packets
466 to make up for the lower data rate.
467 (cf. VCD standard p. IV-6 )*/
469 /* Add the header overhead to the data rate.
470 2279 data bytes per audio pack, 2294 data bytes per video pack*/
471 overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
472 overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
475 /* Add padding so that the full bitrate is 2324*75 bytes/sec */
476 s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate);
479 if (s->is_vcd || s->is_mpeg2)
481 s->pack_header_freq = 1;
483 /* every 2 seconds */
484 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
486 /* the above seems to make pack_header_freq zero sometimes */
487 if (s->pack_header_freq == 0)
488 s->pack_header_freq = 1;
491 /* every 200 packets. Need to look at the spec. */
492 s->system_header_freq = s->pack_header_freq * 40;
494 /* the standard mandates that there are only two system headers
495 in the whole file: one in the first packet of each stream.
496 (see standard p. IV-7 and IV-8) */
497 s->system_header_freq = 0x7fffffff;
499 s->system_header_freq = s->pack_header_freq * 5;
501 for(i=0;i<ctx->nb_streams;i++) {
502 stream = ctx->streams[i]->priv_data;
503 stream->packet_number = 0;
505 s->system_header_size = get_system_header_size(ctx);
506 s->last_scr = AV_NOPTS_VALUE;
509 for(i=0;i<ctx->nb_streams;i++) {
510 av_free(ctx->streams[i]->priv_data);
512 return AVERROR(ENOMEM);
515 static inline void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
519 (((timestamp >> 30) & 0x07) << 1) |
521 avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
522 avio_wb16(pb, (uint16_t)((((timestamp ) & 0x7fff) << 1) | 1));
526 /* return the number of padding bytes that should be inserted into
527 the multiplexed stream.*/
528 static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
530 MpegMuxContext *s = ctx->priv_data;
533 if (s->vcd_padding_bitrate > 0 && pts!=AV_NOPTS_VALUE)
535 int64_t full_pad_bytes;
537 full_pad_bytes = (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0); //FIXME this is wrong
538 pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written);
541 /* might happen if we have already padded to a later timestamp. This
542 can occur if another stream has already advanced further.*/
550 /* Write an MPEG padding packet header. */
551 static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb,int packet_bytes)
553 MpegMuxContext *s = ctx->priv_data;
556 avio_wb32(pb, PADDING_STREAM);
557 avio_wb16(pb, packet_bytes - 6);
564 for(i=0;i<packet_bytes;i++)
568 static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len){
570 PacketDesc *pkt_desc= stream->premux_packet;
573 if(pkt_desc->size == pkt_desc->unwritten_size)
575 len -= pkt_desc->unwritten_size;
576 pkt_desc= pkt_desc->next;
582 /* flush the packet on stream stream_index */
583 static int flush_packet(AVFormatContext *ctx, int stream_index,
584 int64_t pts, int64_t dts, int64_t scr, int trailer_size)
586 MpegMuxContext *s = ctx->priv_data;
587 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
589 int size, payload_size, startcode, id, stuffing_size, i, header_len;
592 int zero_trail_bytes = 0;
593 int pad_packet_bytes = 0;
595 int general_pack = 0; /*"general" pack without data specific to one stream?*/
600 av_dlog(ctx, "packet ID=%2x PTS=%0.3f\n", id, pts / 90000.0);
604 if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) {
605 /* output pack and systems header if needed */
606 size = put_pack_header(ctx, buf_ptr, scr);
611 /* there is exactly one system header for each stream in a VCD MPEG,
612 One in the very first video packet and one in the very first
613 audio packet (see VCD standard p. IV-7 and IV-8).*/
615 if (stream->packet_number==0) {
616 size = put_system_header(ctx, buf_ptr, id);
619 } else if (s->is_dvd) {
620 if (stream->align_iframe || s->packet_number == 0){
621 int PES_bytes_to_fill = s->packet_size - size - 10;
623 if (pts != AV_NOPTS_VALUE) {
625 PES_bytes_to_fill -= 5 + 5;
627 PES_bytes_to_fill -= 5;
630 if (stream->bytes_to_iframe == 0 || s->packet_number == 0) {
631 size = put_system_header(ctx, buf_ptr, 0);
633 size = buf_ptr - buffer;
634 avio_write(ctx->pb, buffer, size);
636 avio_wb32(ctx->pb, PRIVATE_STREAM_2);
637 avio_wb16(ctx->pb, 0x03d4); // length
638 avio_w8(ctx->pb, 0x00); // substream ID, 00=PCI
639 for (i = 0; i < 979; i++)
640 avio_w8(ctx->pb, 0x00);
642 avio_wb32(ctx->pb, PRIVATE_STREAM_2);
643 avio_wb16(ctx->pb, 0x03fa); // length
644 avio_w8(ctx->pb, 0x01); // substream ID, 01=DSI
645 for (i = 0; i < 1017; i++)
646 avio_w8(ctx->pb, 0x00);
648 memset(buffer, 0, 128);
651 stream->align_iframe = 0;
652 scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
653 size = put_pack_header(ctx, buf_ptr, scr);
657 } else if (stream->bytes_to_iframe < PES_bytes_to_fill) {
658 pad_packet_bytes = PES_bytes_to_fill - stream->bytes_to_iframe;
662 if ((s->packet_number % s->system_header_freq) == 0) {
663 size = put_system_header(ctx, buf_ptr, 0);
668 size = buf_ptr - buffer;
669 avio_write(ctx->pb, buffer, size);
671 packet_size = s->packet_size - size;
673 if (s->is_vcd && (id & 0xe0) == AUDIO_ID)
674 /* The VCD standard demands that 20 zero bytes follow
675 each audio pack (see standard p. IV-8).*/
676 zero_trail_bytes += 20;
678 if ((s->is_vcd && stream->packet_number==0)
679 || (s->is_svcd && s->packet_number==0)) {
680 /* for VCD the first pack of each stream contains only the pack header,
681 the system header and lots of padding (see VCD standard p. IV-6).
682 In the case of an audio pack, 20 zero bytes are also added at
684 /* For SVCD we fill the very first pack to increase compatibility with
685 some DVD players. Not mandated by the standard.*/
687 general_pack = 1; /* the system header refers to both streams and no stream data*/
688 pad_packet_bytes = packet_size - zero_trail_bytes;
691 packet_size -= pad_packet_bytes + zero_trail_bytes;
693 if (packet_size > 0) {
695 /* packet header size */
701 if (stream->packet_number==0)
702 header_len += 3; /* PES extension */
703 header_len += 1; /* obligatory stuffing byte */
707 if (pts != AV_NOPTS_VALUE) {
717 payload_size = packet_size - header_len;
719 startcode = PRIVATE_STREAM_1;
727 startcode = 0x100 + id;
730 stuffing_size = payload_size - av_fifo_size(stream->fifo);
732 // first byte does not fit -> reset pts/dts + stuffing
733 if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){
737 if(pts != AV_NOPTS_VALUE)
738 timestamp_len += s->is_mpeg2 ? 5 : 4;
739 pts=dts= AV_NOPTS_VALUE;
740 header_len -= timestamp_len;
741 if (s->is_dvd && stream->align_iframe) {
742 pad_packet_bytes += timestamp_len;
743 packet_size -= timestamp_len;
745 payload_size += timestamp_len;
747 stuffing_size += timestamp_len;
748 if(payload_size > trailer_size)
749 stuffing_size += payload_size - trailer_size;
752 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) { // can't use padding, so use stuffing
753 packet_size += pad_packet_bytes;
754 payload_size += pad_packet_bytes; // undo the previous adjustment
755 if (stuffing_size < 0) {
756 stuffing_size = pad_packet_bytes;
758 stuffing_size += pad_packet_bytes;
760 pad_packet_bytes = 0;
763 if (stuffing_size < 0)
766 if (startcode == PRIVATE_STREAM_1 && id >= 0xa0) {
767 if (payload_size < av_fifo_size(stream->fifo))
768 stuffing_size += payload_size % stream->lpcm_align;
771 if (stuffing_size > 16) { /*<=16 for MPEG-1, <=32 for MPEG-2*/
772 pad_packet_bytes += stuffing_size;
773 packet_size -= stuffing_size;
774 payload_size -= stuffing_size;
778 nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size);
780 avio_wb32(ctx->pb, startcode);
782 avio_wb16(ctx->pb, packet_size);
785 for(i=0;i<stuffing_size;i++)
786 avio_w8(ctx->pb, 0xff);
789 avio_w8(ctx->pb, 0x80); /* mpeg2 id */
793 if (pts != AV_NOPTS_VALUE) {
799 /* Both the MPEG-2 and the SVCD standards demand that the
800 P-STD_buffer_size field be included in the first packet of
801 every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
802 and MPEG-2 standard 2.7.7) */
803 if (stream->packet_number == 0)
806 avio_w8(ctx->pb, pes_flags); /* flags */
807 avio_w8(ctx->pb, header_len - 3 + stuffing_size);
809 if (pes_flags & 0x80) /*write pts*/
810 put_timestamp(ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
811 if (pes_flags & 0x40) /*write dts*/
812 put_timestamp(ctx->pb, 0x01, dts);
814 if (pes_flags & 0x01) { /*write pes extension*/
815 avio_w8(ctx->pb, 0x10); /* flags */
817 /* P-STD buffer info */
818 if ((id & 0xe0) == AUDIO_ID)
819 avio_wb16(ctx->pb, 0x4000 | stream->max_buffer_size/ 128);
821 avio_wb16(ctx->pb, 0x6000 | stream->max_buffer_size/1024);
825 if (pts != AV_NOPTS_VALUE) {
827 put_timestamp(ctx->pb, 0x03, pts);
828 put_timestamp(ctx->pb, 0x01, dts);
830 put_timestamp(ctx->pb, 0x02, pts);
833 avio_w8(ctx->pb, 0x0f);
838 /* special stuffing byte that is always written
839 to prevent accidental generation of start codes. */
840 avio_w8(ctx->pb, 0xff);
842 for(i=0;i<stuffing_size;i++)
843 avio_w8(ctx->pb, 0xff);
846 if (startcode == PRIVATE_STREAM_1) {
847 avio_w8(ctx->pb, id);
849 /* LPCM (XXX: check nb_frames) */
851 avio_wb16(ctx->pb, 4); /* skip 3 header bytes */
852 avio_w8(ctx->pb, stream->lpcm_header[0]);
853 avio_w8(ctx->pb, stream->lpcm_header[1]);
854 avio_w8(ctx->pb, stream->lpcm_header[2]);
855 } else if (id >= 0x40) {
857 avio_w8(ctx->pb, nb_frames);
858 avio_wb16(ctx->pb, trailer_size+1);
863 assert(payload_size - stuffing_size <= av_fifo_size(stream->fifo));
864 av_fifo_generic_read(stream->fifo, ctx->pb, payload_size - stuffing_size, (void*)avio_write);
865 stream->bytes_to_iframe -= payload_size - stuffing_size;
871 if (pad_packet_bytes > 0)
872 put_padding_packet(ctx,ctx->pb, pad_packet_bytes);
874 for(i=0;i<zero_trail_bytes;i++)
875 avio_w8(ctx->pb, 0x00);
881 /* only increase the stream packet number if this pack actually contains
882 something that is specific to this stream! I.e. a dedicated header
885 stream->packet_number++;
887 return payload_size - stuffing_size;
890 static void put_vcd_padding_sector(AVFormatContext *ctx)
892 /* There are two ways to do this padding: writing a sector/pack
893 of 0 values, or writing an MPEG padding pack. Both seem to
894 work with most decoders, BUT the VCD standard only allows a 0-sector
895 (see standard p. IV-4, IV-5).
896 So a 0-sector it is...*/
898 MpegMuxContext *s = ctx->priv_data;
901 for(i=0;i<s->packet_size;i++)
904 s->vcd_padding_bytes_written += s->packet_size;
908 /* increasing the packet number is correct. The SCR of the following packs
909 is calculated from the packet_number and it has to include the padding
910 sector (it represents the sector index, not the MPEG pack index)
911 (see VCD standard p. IV-6)*/
915 static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){
916 // MpegMuxContext *s = ctx->priv_data;
919 for(i=0; i<ctx->nb_streams; i++){
920 AVStream *st = ctx->streams[i];
921 StreamInfo *stream = st->priv_data;
922 PacketDesc *pkt_desc;
924 while((pkt_desc= stream->predecode_packet)
925 && scr > pkt_desc->dts){ //FIXME > vs >=
926 if(stream->buffer_index < pkt_desc->size ||
927 stream->predecode_packet == stream->premux_packet){
928 av_log(ctx, AV_LOG_ERROR,
929 "buffer underflow st=%d bufi=%d size=%d\n",
930 i, stream->buffer_index, pkt_desc->size);
933 stream->buffer_index -= pkt_desc->size;
935 stream->predecode_packet= pkt_desc->next;
943 static int output_packet(AVFormatContext *ctx, int flush){
944 MpegMuxContext *s = ctx->priv_data;
947 int i, avail_space=0, es_size, trailer_size;
949 int best_score= INT_MIN;
950 int ignore_constraints=0;
951 int64_t scr= s->last_scr;
952 PacketDesc *timestamp_packet;
953 const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
956 for(i=0; i<ctx->nb_streams; i++){
957 AVStream *st = ctx->streams[i];
958 StreamInfo *stream = st->priv_data;
959 const int avail_data= av_fifo_size(stream->fifo);
960 const int space= stream->max_buffer_size - stream->buffer_index;
961 int rel_space= 1024LL*space / stream->max_buffer_size;
962 PacketDesc *next_pkt= stream->premux_packet;
964 /* for subtitle, a single PES packet must be generated,
965 so we flush after every single subtitle packet */
966 if(s->packet_size > avail_data && !flush
967 && st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE)
971 av_assert0(avail_data>0);
973 if(space < s->packet_size && !ignore_constraints)
976 if(next_pkt && next_pkt->dts - scr > max_delay)
978 if ( stream->predecode_packet
979 && stream->predecode_packet->size > stream->buffer_index)
981 if(rel_space > best_score){
982 best_score= rel_space;
989 int64_t best_dts= INT64_MAX;
991 for(i=0; i<ctx->nb_streams; i++){
992 AVStream *st = ctx->streams[i];
993 StreamInfo *stream = st->priv_data;
994 PacketDesc *pkt_desc= stream->predecode_packet;
995 if(pkt_desc && pkt_desc->dts < best_dts)
996 best_dts= pkt_desc->dts;
999 av_dlog(ctx, "bumping scr, scr:%f, dts:%f\n",
1000 scr / 90000.0, best_dts / 90000.0);
1001 if(best_dts == INT64_MAX)
1004 if(scr >= best_dts+1 && !ignore_constraints){
1005 av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n");
1006 ignore_constraints= 1;
1008 scr= FFMAX(best_dts+1, scr);
1009 if(remove_decoded_packets(ctx, scr) < 0)
1014 assert(best_i >= 0);
1016 st = ctx->streams[best_i];
1017 stream = st->priv_data;
1019 assert(av_fifo_size(stream->fifo) > 0);
1021 assert(avail_space >= s->packet_size || ignore_constraints);
1023 timestamp_packet= stream->premux_packet;
1024 if(timestamp_packet->unwritten_size == timestamp_packet->size){
1027 trailer_size= timestamp_packet->unwritten_size;
1028 timestamp_packet= timestamp_packet->next;
1031 if(timestamp_packet){
1032 av_dlog(ctx, "dts:%f pts:%f scr:%f stream:%d\n",
1033 timestamp_packet->dts / 90000.0,
1034 timestamp_packet->pts / 90000.0,
1035 scr / 90000.0, best_i);
1036 es_size= flush_packet(ctx, best_i, timestamp_packet->pts, timestamp_packet->dts, scr, trailer_size);
1038 assert(av_fifo_size(stream->fifo) == trailer_size);
1039 es_size= flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr, trailer_size);
1043 /* Write one or more padding sectors, if necessary, to reach
1044 the constant overall bitrate.*/
1047 while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->premux_packet->pts) ) >= s->packet_size){ //FIXME pts cannot be correct here
1048 put_vcd_padding_sector(ctx);
1049 s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
1053 stream->buffer_index += es_size;
1054 s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
1056 while(stream->premux_packet && stream->premux_packet->unwritten_size <= es_size){
1057 es_size -= stream->premux_packet->unwritten_size;
1058 stream->premux_packet= stream->premux_packet->next;
1061 stream->premux_packet->unwritten_size -= es_size;
1063 if(remove_decoded_packets(ctx, s->last_scr) < 0)
1069 static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
1071 MpegMuxContext *s = ctx->priv_data;
1072 int stream_index= pkt->stream_index;
1073 int size= pkt->size;
1074 uint8_t *buf= pkt->data;
1075 AVStream *st = ctx->streams[stream_index];
1076 StreamInfo *stream = st->priv_data;
1078 PacketDesc *pkt_desc;
1080 const int is_iframe = st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (pkt->flags & AV_PKT_FLAG_KEY);
1082 preload = av_rescale(s->preload, 90000, AV_TIME_BASE);
1087 if (s->last_scr == AV_NOPTS_VALUE) {
1088 if (dts == AV_NOPTS_VALUE || (dts < preload && ctx->avoid_negative_ts) || s->is_dvd) {
1089 if (dts != AV_NOPTS_VALUE)
1090 s->preload += av_rescale(-dts, AV_TIME_BASE, 90000);
1093 s->last_scr = dts - preload;
1096 preload = av_rescale(s->preload, 90000, AV_TIME_BASE);
1097 av_log(ctx, AV_LOG_DEBUG, "First SCR: %"PRId64" First DTS: %"PRId64"\n", s->last_scr, dts + preload);
1100 if (dts != AV_NOPTS_VALUE) dts += preload;
1101 if (pts != AV_NOPTS_VALUE) pts += preload;
1103 av_dlog(ctx, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n",
1104 dts / 90000.0, pts / 90000.0, pkt->flags,
1105 pkt->stream_index, pts != AV_NOPTS_VALUE);
1106 if (!stream->premux_packet)
1107 stream->next_packet = &stream->premux_packet;
1108 *stream->next_packet=
1109 pkt_desc= av_mallocz(sizeof(PacketDesc));
1112 pkt_desc->unwritten_size=
1113 pkt_desc->size= size;
1114 if(!stream->predecode_packet)
1115 stream->predecode_packet= pkt_desc;
1116 stream->next_packet= &pkt_desc->next;
1118 if (av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size) < 0)
1122 if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) { // min VOBU length 0.4 seconds (mpucoder)
1123 stream->bytes_to_iframe = av_fifo_size(stream->fifo);
1124 stream->align_iframe = 1;
1125 stream->vobu_start_pts = pts;
1129 av_fifo_generic_write(stream->fifo, buf, size, NULL);
1132 int ret= output_packet(ctx, 0);
1138 static int mpeg_mux_end(AVFormatContext *ctx)
1140 // MpegMuxContext *s = ctx->priv_data;
1145 int ret= output_packet(ctx, 1);
1152 /* End header according to MPEG1 systems standard. We do not write
1153 it as it is usually not needed by decoders and because it
1154 complicates MPEG stream concatenation. */
1155 //avio_wb32(ctx->pb, ISO_11172_END_CODE);
1156 //avio_flush(ctx->pb);
1158 for(i=0;i<ctx->nb_streams;i++) {
1159 stream = ctx->streams[i]->priv_data;
1161 assert(av_fifo_size(stream->fifo) == 0);
1162 av_fifo_freep(&stream->fifo);
1167 #define OFFSET(x) offsetof(MpegMuxContext, x)
1168 #define E AV_OPT_FLAG_ENCODING_PARAM
1169 static const AVOption options[] = {
1170 { "muxrate", NULL, OFFSET(user_mux_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, ((1<<22) - 1) * (8 * 50), E },
1171 { "preload", "Initial demux-decode delay in microseconds.", OFFSET(preload), AV_OPT_TYPE_INT, {.i64 = 500000}, 0, INT_MAX, E},
1175 #define MPEGENC_CLASS(flavor)\
1176 static const AVClass flavor ## _class = {\
1177 .class_name = #flavor " muxer",\
1178 .item_name = av_default_item_name,\
1179 .version = LIBAVUTIL_VERSION_INT,\
1183 #if CONFIG_MPEG1SYSTEM_MUXER
1185 AVOutputFormat ff_mpeg1system_muxer = {
1187 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream"),
1188 .mime_type = "video/mpeg",
1189 .extensions = "mpg,mpeg",
1190 .priv_data_size = sizeof(MpegMuxContext),
1191 .audio_codec = AV_CODEC_ID_MP2,
1192 .video_codec = AV_CODEC_ID_MPEG1VIDEO,
1193 .write_header = mpeg_mux_init,
1194 .write_packet = mpeg_mux_write_packet,
1195 .write_trailer = mpeg_mux_end,
1196 .priv_class = &mpeg_class,
1199 #if CONFIG_MPEG1VCD_MUXER
1201 AVOutputFormat ff_mpeg1vcd_muxer = {
1203 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream (VCD)"),
1204 .mime_type = "video/mpeg",
1205 .priv_data_size = sizeof(MpegMuxContext),
1206 .audio_codec = AV_CODEC_ID_MP2,
1207 .video_codec = AV_CODEC_ID_MPEG1VIDEO,
1208 .write_header = mpeg_mux_init,
1209 .write_packet = mpeg_mux_write_packet,
1210 .write_trailer = mpeg_mux_end,
1211 .priv_class = &vcd_class,
1214 #if CONFIG_MPEG2VOB_MUXER
1216 AVOutputFormat ff_mpeg2vob_muxer = {
1218 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (VOB)"),
1219 .mime_type = "video/mpeg",
1220 .extensions = "vob",
1221 .priv_data_size = sizeof(MpegMuxContext),
1222 .audio_codec = AV_CODEC_ID_MP2,
1223 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1224 .write_header = mpeg_mux_init,
1225 .write_packet = mpeg_mux_write_packet,
1226 .write_trailer = mpeg_mux_end,
1227 .priv_class = &vob_class,
1231 /* Same as mpeg2vob_mux except that the pack size is 2324 */
1232 #if CONFIG_MPEG2SVCD_MUXER
1234 AVOutputFormat ff_mpeg2svcd_muxer = {
1236 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (SVCD)"),
1237 .mime_type = "video/mpeg",
1238 .extensions = "vob",
1239 .priv_data_size = sizeof(MpegMuxContext),
1240 .audio_codec = AV_CODEC_ID_MP2,
1241 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1242 .write_header = mpeg_mux_init,
1243 .write_packet = mpeg_mux_write_packet,
1244 .write_trailer = mpeg_mux_end,
1245 .priv_class = &svcd_class,
1249 /* Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
1250 #if CONFIG_MPEG2DVD_MUXER
1252 AVOutputFormat ff_mpeg2dvd_muxer = {
1254 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (DVD VOB)"),
1255 .mime_type = "video/mpeg",
1256 .extensions = "dvd",
1257 .priv_data_size = sizeof(MpegMuxContext),
1258 .audio_codec = AV_CODEC_ID_MP2,
1259 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1260 .write_header = mpeg_mux_init,
1261 .write_packet = mpeg_mux_write_packet,
1262 .write_trailer = mpeg_mux_end,
1263 .priv_class = &dvd_class,