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"
30 #include "libavcodec/put_bits.h"
36 #define MAX_PAYLOAD_SIZE 4096
38 typedef struct PacketDesc {
44 struct PacketDesc *next;
50 int max_buffer_size; /* in bytes */
52 PacketDesc *predecode_packet;
53 PacketDesc *premux_packet;
54 PacketDesc **next_packet;
56 uint8_t lpcm_header[3];
60 int64_t vobu_start_pts;
65 int packet_size; /* required packet size */
67 int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
68 int system_header_freq;
69 int system_header_size;
70 int user_mux_rate; /* bitrate in units of bits/s */
71 int mux_rate; /* bitrate in units of 50 bytes/s */
79 int64_t last_scr; /* current system clock */
81 double vcd_padding_bitrate; // FIXME floats
82 int64_t vcd_padding_bytes_written;
87 extern AVOutputFormat ff_mpeg1vcd_muxer;
88 extern AVOutputFormat ff_mpeg2dvd_muxer;
89 extern AVOutputFormat ff_mpeg2svcd_muxer;
90 extern AVOutputFormat ff_mpeg2vob_muxer;
92 static int put_pack_header(AVFormatContext *ctx, uint8_t *buf,
95 MpegMuxContext *s = ctx->priv_data;
98 init_put_bits(&pb, buf, 128);
100 put_bits32(&pb, PACK_START_CODE);
102 put_bits(&pb, 2, 0x1);
104 put_bits(&pb, 4, 0x2);
105 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
107 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
109 put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
112 /* clock extension */
115 put_bits(&pb, 22, s->mux_rate);
119 put_bits(&pb, 5, 0x1f); /* reserved */
120 put_bits(&pb, 3, 0); /* stuffing length */
123 return put_bits_ptr(&pb) - pb.buf;
126 static int put_system_header(AVFormatContext *ctx, uint8_t *buf,
127 int only_for_stream_id)
129 MpegMuxContext *s = ctx->priv_data;
130 int size, i, private_stream_coded, id;
133 init_put_bits(&pb, buf, 128);
135 put_bits32(&pb, SYSTEM_HEADER_START_CODE);
136 put_bits(&pb, 16, 0);
139 /* maximum bit rate of the multiplexed stream */
140 put_bits(&pb, 22, s->mux_rate);
141 put_bits(&pb, 1, 1); /* marker */
142 if (s->is_vcd && only_for_stream_id == VIDEO_ID) {
143 /* This header applies only to the video stream
144 * (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
171 * (see VCD standard p. IV-7) */
174 put_bits(&pb, 5, s->video_bound);
177 put_bits(&pb, 1, 0); /* packet_rate_restriction_flag */
178 put_bits(&pb, 7, 0x7f); /* reserved byte */
180 put_bits(&pb, 8, 0xff); /* reserved byte */
182 /* DVD-Video Stream_bound entries
183 * id (0xB9) video, maximum P-STD for stream 0xE0. (P-STD_buffer_bound_scale = 1)
184 * 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)
185 * id (0xBD) private stream 1 (audio other than MPEG and subpictures). (P-STD_buffer_bound_scale = 1)
186 * id (0xBF) private stream 2, NAV packs, set to 2x1024. */
189 int P_STD_max_video = 0;
190 int P_STD_max_mpeg_audio = 0;
191 int P_STD_max_mpeg_PS1 = 0;
193 for (i = 0; i < ctx->nb_streams; i++) {
194 StreamInfo *stream = ctx->streams[i]->priv_data;
197 if (id == 0xbd && stream->max_buffer_size > P_STD_max_mpeg_PS1) {
198 P_STD_max_mpeg_PS1 = stream->max_buffer_size;
199 } else if (id >= 0xc0 && id <= 0xc7 &&
200 stream->max_buffer_size > P_STD_max_mpeg_audio) {
201 P_STD_max_mpeg_audio = stream->max_buffer_size;
202 } else if (id == 0xe0 &&
203 stream->max_buffer_size > P_STD_max_video) {
204 P_STD_max_video = stream->max_buffer_size;
209 put_bits(&pb, 8, 0xb9); /* stream ID */
212 put_bits(&pb, 13, P_STD_max_video / 1024);
215 if (P_STD_max_mpeg_audio == 0)
216 P_STD_max_mpeg_audio = 4096;
217 put_bits(&pb, 8, 0xb8); /* stream ID */
220 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
222 /* private stream 1 */
223 put_bits(&pb, 8, 0xbd); /* stream ID */
226 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
228 /* private stream 2 */
229 put_bits(&pb, 8, 0xbf); /* stream ID */
232 put_bits(&pb, 13, 2);
234 /* audio stream info */
235 private_stream_coded = 0;
236 for (i = 0; i < ctx->nb_streams; i++) {
237 StreamInfo *stream = ctx->streams[i]->priv_data;
239 /* For VCDs, only include the stream info for the stream
240 * that the pack which contains this system belongs to.
241 * (see VCD standard p. IV-7) */
242 if (!s->is_vcd || stream->id == only_for_stream_id ||
243 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, h264_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;
341 for (i = 0; i < ctx->nb_streams; i++) {
342 st = ctx->streams[i];
343 stream = av_mallocz(sizeof(StreamInfo));
346 st->priv_data = stream;
348 avpriv_set_pts_info(st, 64, 1, 90000);
350 switch (st->codec->codec_type) {
351 case AVMEDIA_TYPE_AUDIO:
353 (st->codec->codec_id == AV_CODEC_ID_AC3 ||
354 st->codec->codec_id == AV_CODEC_ID_DTS ||
355 st->codec->codec_id == AV_CODEC_ID_PCM_S16BE))
356 av_log(ctx, AV_LOG_WARNING,
357 "%s in MPEG-1 system streams is not widely supported, "
358 "consider using the vob or the dvd muxer "
359 "to force a MPEG-2 program stream.\n",
360 avcodec_get_name(st->codec->codec_id));
361 if (st->codec->codec_id == AV_CODEC_ID_AC3) {
362 stream->id = ac3_id++;
363 } else if (st->codec->codec_id == AV_CODEC_ID_DTS) {
364 stream->id = dts_id++;
365 } else if (st->codec->codec_id == AV_CODEC_ID_PCM_S16BE) {
366 stream->id = lpcm_id++;
367 for (j = 0; j < 4; j++) {
368 if (lpcm_freq_tab[j] == st->codec->sample_rate)
373 if (st->codec->channels > 8)
375 stream->lpcm_header[0] = 0x0c;
376 stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4);
377 stream->lpcm_header[2] = 0x80;
378 stream->lpcm_align = st->codec->channels * 2;
380 stream->id = mpa_id++;
383 /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
384 * Right now it is also used for everything else. */
385 stream->max_buffer_size = 4 * 1024;
388 case AVMEDIA_TYPE_VIDEO:
389 if (st->codec->codec_id == AV_CODEC_ID_H264)
390 stream->id = h264_id++;
392 stream->id = mpv_id++;
393 if (st->codec->rc_buffer_size)
394 stream->max_buffer_size = 6 * 1024 + st->codec->rc_buffer_size / 8;
396 av_log(ctx, AV_LOG_WARNING,
397 "VBV buffer size not set, using default size of 130KB\n"
398 "If you want the mpeg file to be compliant to some specification\n"
399 "Like DVD, VCD or others, make sure you set the correct buffer size\n");
400 // FIXME: this is probably too small as default
401 stream->max_buffer_size = 230 * 1024;
403 if (stream->max_buffer_size > 1024 * 8191) {
404 av_log(ctx, AV_LOG_WARNING, "buffer size %d, too large\n", stream->max_buffer_size);
405 stream->max_buffer_size = 1024 * 8191;
409 case AVMEDIA_TYPE_SUBTITLE:
410 stream->id = mps_id++;
411 stream->max_buffer_size = 16 * 1024;
416 stream->fifo = av_fifo_alloc(16);
423 for (i = 0; i < ctx->nb_streams; i++) {
425 st = ctx->streams[i];
426 stream = (StreamInfo *)st->priv_data;
428 if (st->codec->rc_max_rate ||
429 st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
430 codec_rate = st->codec->rc_max_rate;
432 codec_rate = st->codec->bit_rate;
435 codec_rate = (1 << 21) * 8 * 50 / ctx->nb_streams;
437 bitrate += codec_rate;
439 if ((stream->id & 0xe0) == AUDIO_ID)
440 audio_bitrate += codec_rate;
441 else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
442 video_bitrate += codec_rate;
445 if (s->user_mux_rate) {
446 s->mux_rate = (s->user_mux_rate + (8 * 50) - 1) / (8 * 50);
448 /* we increase slightly the bitrate to take into account the
449 * headers. XXX: compute it exactly */
450 bitrate += bitrate / 20;
452 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
453 if (s->mux_rate >= (1<<22)) {
454 av_log(ctx, AV_LOG_WARNING, "mux rate %d is too large\n", s->mux_rate);
455 s->mux_rate = (1<<22) - 1;
460 double overhead_rate;
462 /* The VCD standard mandates that the mux_rate field is 3528
463 * (see standard p. IV-6).
464 * The value is actually "wrong", i.e. if you calculate
465 * it using the normal formula and the 75 sectors per second transfer
466 * rate you get a different value because the real pack size is 2324,
467 * not 2352. But the standard explicitly specifies that the mux_rate
468 * field in the header must have this value. */
469 // s->mux_rate = 2352 * 75 / 50; /* = 3528 */
471 /* The VCD standard states that the muxed stream must be
472 * exactly 75 packs / second (the data rate of a single speed cdrom).
473 * Since the video bitrate (probably 1150000 bits/sec) will be below
474 * the theoretical maximum we have to add some padding packets
475 * to make up for the lower data rate.
476 * (cf. VCD standard p. IV-6 ) */
478 /* Add the header overhead to the data rate.
479 * 2279 data bytes per audio pack, 2294 data bytes per video pack */
480 overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
481 overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
484 /* Add padding so that the full bitrate is 2324*75 bytes/sec */
485 s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate);
488 if (s->is_vcd || s->is_mpeg2)
490 s->pack_header_freq = 1;
492 /* every 2 seconds */
493 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
495 /* the above seems to make pack_header_freq zero sometimes */
496 if (s->pack_header_freq == 0)
497 s->pack_header_freq = 1;
500 /* every 200 packets. Need to look at the spec. */
501 s->system_header_freq = s->pack_header_freq * 40;
503 /* the standard mandates that there are only two system headers
504 * in the whole file: one in the first packet of each stream.
505 * (see standard p. IV-7 and IV-8) */
506 s->system_header_freq = 0x7fffffff;
508 s->system_header_freq = s->pack_header_freq * 5;
510 for (i = 0; i < ctx->nb_streams; i++) {
511 stream = ctx->streams[i]->priv_data;
512 stream->packet_number = 0;
514 s->system_header_size = get_system_header_size(ctx);
515 s->last_scr = AV_NOPTS_VALUE;
519 for (i = 0; i < ctx->nb_streams; i++)
520 av_freep(&ctx->streams[i]->priv_data);
521 return AVERROR(ENOMEM);
524 static inline void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
526 avio_w8(pb, (id << 4) | (((timestamp >> 30) & 0x07) << 1) | 1);
527 avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
528 avio_wb16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
531 /* return the number of padding bytes that should be inserted into
532 * the multiplexed stream. */
533 static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
535 MpegMuxContext *s = ctx->priv_data;
538 if (s->vcd_padding_bitrate > 0 && pts != AV_NOPTS_VALUE) {
539 int64_t full_pad_bytes;
541 // FIXME: this is wrong
543 (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0);
544 pad_bytes = (int)(full_pad_bytes - s->vcd_padding_bytes_written);
547 /* might happen if we have already padded to a later timestamp. This
548 * can occur if another stream has already advanced further. */
555 /* Write an MPEG padding packet header. */
556 static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb,
559 MpegMuxContext *s = ctx->priv_data;
562 avio_wb32(pb, PADDING_STREAM);
563 avio_wb16(pb, packet_bytes - 6);
570 for (i = 0; i < packet_bytes; i++)
574 static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len)
577 PacketDesc *pkt_desc = stream->premux_packet;
580 if (pkt_desc->size == pkt_desc->unwritten_size)
582 len -= pkt_desc->unwritten_size;
583 pkt_desc = pkt_desc->next;
589 /* flush the packet on stream stream_index */
590 static int flush_packet(AVFormatContext *ctx, int stream_index,
591 int64_t pts, int64_t dts, int64_t scr, int trailer_size)
593 MpegMuxContext *s = ctx->priv_data;
594 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
596 int size, payload_size, startcode, id, stuffing_size, i, header_len;
599 int zero_trail_bytes = 0;
600 int pad_packet_bytes = 0;
602 /* "general" pack without data specific to one stream? */
603 int general_pack = 0;
608 av_dlog(ctx, "packet ID=%2x PTS=%0.3f\n", id, pts / 90000.0);
612 if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) {
613 /* output pack and systems header if needed */
614 size = put_pack_header(ctx, buf_ptr, scr);
619 /* there is exactly one system header for each stream in a VCD MPEG,
620 * One in the very first video packet and one in the very first
621 * audio packet (see VCD standard p. IV-7 and IV-8). */
623 if (stream->packet_number == 0) {
624 size = put_system_header(ctx, buf_ptr, id);
627 } else if (s->is_dvd) {
628 if (stream->align_iframe || s->packet_number == 0) {
629 int PES_bytes_to_fill = s->packet_size - size - 10;
631 if (pts != AV_NOPTS_VALUE) {
633 PES_bytes_to_fill -= 5 + 5;
635 PES_bytes_to_fill -= 5;
638 if (stream->bytes_to_iframe == 0 || s->packet_number == 0) {
639 size = put_system_header(ctx, buf_ptr, 0);
641 size = buf_ptr - buffer;
642 avio_write(ctx->pb, buffer, size);
644 avio_wb32(ctx->pb, PRIVATE_STREAM_2);
645 avio_wb16(ctx->pb, 0x03d4); // length
646 avio_w8(ctx->pb, 0x00); // substream ID, 00=PCI
647 for (i = 0; i < 979; i++)
648 avio_w8(ctx->pb, 0x00);
650 avio_wb32(ctx->pb, PRIVATE_STREAM_2);
651 avio_wb16(ctx->pb, 0x03fa); // length
652 avio_w8(ctx->pb, 0x01); // substream ID, 01=DSI
653 for (i = 0; i < 1017; i++)
654 avio_w8(ctx->pb, 0x00);
656 memset(buffer, 0, 128);
659 stream->align_iframe = 0;
660 // FIXME: rounding and first few bytes of each packet
661 scr += s->packet_size * 90000LL /
662 (s->mux_rate * 50LL);
663 size = put_pack_header(ctx, buf_ptr, scr);
667 } else if (stream->bytes_to_iframe < PES_bytes_to_fill) {
668 pad_packet_bytes = PES_bytes_to_fill -
669 stream->bytes_to_iframe;
673 if ((s->packet_number % s->system_header_freq) == 0) {
674 size = put_system_header(ctx, buf_ptr, 0);
679 size = buf_ptr - buffer;
680 avio_write(ctx->pb, buffer, size);
682 packet_size = s->packet_size - size;
684 if (s->is_vcd && (id & 0xe0) == AUDIO_ID)
685 /* The VCD standard demands that 20 zero bytes follow
686 * each audio pack (see standard p. IV-8). */
687 zero_trail_bytes += 20;
689 if ((s->is_vcd && stream->packet_number == 0) ||
690 (s->is_svcd && s->packet_number == 0)) {
691 /* for VCD the first pack of each stream contains only the pack header,
692 * the system header and lots of padding (see VCD standard p. IV-6).
693 * In the case of an audio pack, 20 zero bytes are also added at
695 /* For SVCD we fill the very first pack to increase compatibility with
696 * some DVD players. Not mandated by the standard. */
698 /* the system header refers to both streams and no stream data */
700 pad_packet_bytes = packet_size - zero_trail_bytes;
703 packet_size -= pad_packet_bytes + zero_trail_bytes;
705 if (packet_size > 0) {
706 /* packet header size */
712 if (stream->packet_number == 0)
713 header_len += 3; /* PES extension */
714 header_len += 1; /* obligatory stuffing byte */
718 if (pts != AV_NOPTS_VALUE) {
728 payload_size = packet_size - header_len;
730 startcode = PRIVATE_STREAM_1;
738 startcode = 0x100 + id;
741 stuffing_size = payload_size - av_fifo_size(stream->fifo);
743 // first byte does not fit -> reset pts/dts + stuffing
744 if (payload_size <= trailer_size && pts != AV_NOPTS_VALUE) {
745 int timestamp_len = 0;
748 if (pts != AV_NOPTS_VALUE)
749 timestamp_len += s->is_mpeg2 ? 5 : 4;
751 dts = AV_NOPTS_VALUE;
752 header_len -= timestamp_len;
753 if (s->is_dvd && stream->align_iframe) {
754 pad_packet_bytes += timestamp_len;
755 packet_size -= timestamp_len;
757 payload_size += timestamp_len;
759 stuffing_size += timestamp_len;
760 if (payload_size > trailer_size)
761 stuffing_size += payload_size - trailer_size;
764 // can't use padding, so use stuffing
765 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) {
766 packet_size += pad_packet_bytes;
767 payload_size += pad_packet_bytes; // undo the previous adjustment
768 if (stuffing_size < 0)
769 stuffing_size = pad_packet_bytes;
771 stuffing_size += pad_packet_bytes;
772 pad_packet_bytes = 0;
775 if (stuffing_size < 0)
778 if (startcode == PRIVATE_STREAM_1 && id >= 0xa0) {
779 if (payload_size < av_fifo_size(stream->fifo))
780 stuffing_size += payload_size % stream->lpcm_align;
783 if (stuffing_size > 16) { /* <=16 for MPEG-1, <=32 for MPEG-2 */
784 pad_packet_bytes += stuffing_size;
785 packet_size -= stuffing_size;
786 payload_size -= stuffing_size;
790 nb_frames = get_nb_frames(ctx, stream, payload_size - stuffing_size);
792 avio_wb32(ctx->pb, startcode);
794 avio_wb16(ctx->pb, packet_size);
797 for (i = 0; i < stuffing_size; i++)
798 avio_w8(ctx->pb, 0xff);
801 avio_w8(ctx->pb, 0x80); /* mpeg2 id */
805 if (pts != AV_NOPTS_VALUE) {
811 /* Both the MPEG-2 and the SVCD standards demand that the
812 * P-STD_buffer_size field be included in the first packet of
813 * every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
814 * and MPEG-2 standard 2.7.7) */
815 if (stream->packet_number == 0)
818 avio_w8(ctx->pb, pes_flags); /* flags */
819 avio_w8(ctx->pb, header_len - 3 + stuffing_size);
821 if (pes_flags & 0x80) /* write pts */
822 put_timestamp(ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
823 if (pes_flags & 0x40) /* write dts */
824 put_timestamp(ctx->pb, 0x01, dts);
826 if (pes_flags & 0x01) { /* write pes extension */
827 avio_w8(ctx->pb, 0x10); /* flags */
829 /* P-STD buffer info */
830 if ((id & 0xe0) == AUDIO_ID)
831 avio_wb16(ctx->pb, 0x4000 | stream->max_buffer_size / 128);
833 avio_wb16(ctx->pb, 0x6000 | stream->max_buffer_size / 1024);
836 if (pts != AV_NOPTS_VALUE) {
838 put_timestamp(ctx->pb, 0x03, pts);
839 put_timestamp(ctx->pb, 0x01, dts);
841 put_timestamp(ctx->pb, 0x02, pts);
844 avio_w8(ctx->pb, 0x0f);
849 /* special stuffing byte that is always written
850 * to prevent accidental generation of start codes. */
851 avio_w8(ctx->pb, 0xff);
853 for (i = 0; i < stuffing_size; i++)
854 avio_w8(ctx->pb, 0xff);
857 if (startcode == PRIVATE_STREAM_1) {
858 avio_w8(ctx->pb, id);
860 /* LPCM (XXX: check nb_frames) */
862 avio_wb16(ctx->pb, 4); /* skip 3 header bytes */
863 avio_w8(ctx->pb, stream->lpcm_header[0]);
864 avio_w8(ctx->pb, stream->lpcm_header[1]);
865 avio_w8(ctx->pb, stream->lpcm_header[2]);
866 } else if (id >= 0x40) {
868 avio_w8(ctx->pb, nb_frames);
869 avio_wb16(ctx->pb, trailer_size + 1);
874 av_assert0(payload_size - stuffing_size <= av_fifo_size(stream->fifo));
875 av_fifo_generic_read(stream->fifo, ctx->pb,
876 payload_size - stuffing_size,
877 (void (*)(void*, void*, int))avio_write);
878 stream->bytes_to_iframe -= payload_size - stuffing_size;
884 if (pad_packet_bytes > 0)
885 put_padding_packet(ctx, ctx->pb, pad_packet_bytes);
887 for (i = 0; i < zero_trail_bytes; i++)
888 avio_w8(ctx->pb, 0x00);
894 /* only increase the stream packet number if this pack actually contains
895 * something that is specific to this stream! I.e. a dedicated header
898 stream->packet_number++;
900 return payload_size - stuffing_size;
903 static void put_vcd_padding_sector(AVFormatContext *ctx)
905 /* There are two ways to do this padding: writing a sector/pack
906 * of 0 values, or writing an MPEG padding pack. Both seem to
907 * work with most decoders, BUT the VCD standard only allows a 0-sector
908 * (see standard p. IV-4, IV-5).
909 * So a 0-sector it is... */
911 MpegMuxContext *s = ctx->priv_data;
914 for (i = 0; i < s->packet_size; i++)
917 s->vcd_padding_bytes_written += s->packet_size;
921 /* increasing the packet number is correct. The SCR of the following packs
922 * is calculated from the packet_number and it has to include the padding
923 * sector (it represents the sector index, not the MPEG pack index)
924 * (see VCD standard p. IV-6) */
928 static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr)
932 for (i = 0; i < ctx->nb_streams; i++) {
933 AVStream *st = ctx->streams[i];
934 StreamInfo *stream = st->priv_data;
935 PacketDesc *pkt_desc;
937 while ((pkt_desc = stream->predecode_packet) &&
938 scr > pkt_desc->dts) { // FIXME: > vs >=
939 if (stream->buffer_index < pkt_desc->size ||
940 stream->predecode_packet == stream->premux_packet) {
941 av_log(ctx, AV_LOG_ERROR,
942 "buffer underflow st=%d bufi=%d size=%d\n",
943 i, stream->buffer_index, pkt_desc->size);
946 stream->buffer_index -= pkt_desc->size;
947 stream->predecode_packet = pkt_desc->next;
955 static int output_packet(AVFormatContext *ctx, int flush)
957 MpegMuxContext *s = ctx->priv_data;
960 int i, avail_space = 0, es_size, trailer_size;
962 int best_score = INT_MIN;
963 int ignore_constraints = 0;
964 int64_t scr = s->last_scr;
965 PacketDesc *timestamp_packet;
966 const int64_t max_delay = av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
969 for (i = 0; i < ctx->nb_streams; i++) {
970 AVStream *st = ctx->streams[i];
971 StreamInfo *stream = st->priv_data;
972 const int avail_data = av_fifo_size(stream->fifo);
973 const int space = stream->max_buffer_size - stream->buffer_index;
974 int rel_space = 1024LL * space / stream->max_buffer_size;
975 PacketDesc *next_pkt = stream->premux_packet;
977 /* for subtitle, a single PES packet must be generated,
978 * so we flush after every single subtitle packet */
979 if (s->packet_size > avail_data && !flush
980 && st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE)
984 av_assert0(avail_data > 0);
986 if (space < s->packet_size && !ignore_constraints)
989 if (next_pkt && next_pkt->dts - scr > max_delay)
991 if ( stream->predecode_packet
992 && stream->predecode_packet->size > stream->buffer_index)
994 if (rel_space > best_score) {
995 best_score = rel_space;
1002 int64_t best_dts = INT64_MAX;
1004 for (i = 0; i < ctx->nb_streams; i++) {
1005 AVStream *st = ctx->streams[i];
1006 StreamInfo *stream = st->priv_data;
1007 PacketDesc *pkt_desc = stream->predecode_packet;
1008 if (pkt_desc && pkt_desc->dts < best_dts)
1009 best_dts = pkt_desc->dts;
1012 av_dlog(ctx, "bumping scr, scr:%f, dts:%f\n",
1013 scr / 90000.0, best_dts / 90000.0);
1014 if (best_dts == INT64_MAX)
1017 if (scr >= best_dts + 1 && !ignore_constraints) {
1018 av_log(ctx, AV_LOG_ERROR,
1019 "packet too large, ignoring buffer limits to mux it\n");
1020 ignore_constraints = 1;
1022 scr = FFMAX(best_dts + 1, scr);
1023 if (remove_decoded_packets(ctx, scr) < 0)
1028 av_assert0(best_i >= 0);
1030 st = ctx->streams[best_i];
1031 stream = st->priv_data;
1033 av_assert0(av_fifo_size(stream->fifo) > 0);
1035 av_assert0(avail_space >= s->packet_size || ignore_constraints);
1037 timestamp_packet = stream->premux_packet;
1038 if (timestamp_packet->unwritten_size == timestamp_packet->size) {
1041 trailer_size = timestamp_packet->unwritten_size;
1042 timestamp_packet = timestamp_packet->next;
1045 if (timestamp_packet) {
1046 av_dlog(ctx, "dts:%f pts:%f scr:%f stream:%d\n",
1047 timestamp_packet->dts / 90000.0,
1048 timestamp_packet->pts / 90000.0,
1049 scr / 90000.0, best_i);
1050 es_size = flush_packet(ctx, best_i, timestamp_packet->pts,
1051 timestamp_packet->dts, scr, trailer_size);
1053 av_assert0(av_fifo_size(stream->fifo) == trailer_size);
1054 es_size = flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr,
1059 /* Write one or more padding sectors, if necessary, to reach
1060 * the constant overall bitrate. */
1063 // FIXME: pts cannot be correct here
1064 while ((vcd_pad_bytes = get_vcd_padding_size(ctx, stream->premux_packet->pts)) >= s->packet_size) {
1065 put_vcd_padding_sector(ctx);
1066 // FIXME: rounding and first few bytes of each packet
1067 s->last_scr += s->packet_size * 90000LL / (s->mux_rate * 50LL);
1071 stream->buffer_index += es_size;
1072 // FIXME: rounding and first few bytes of each packet
1073 s->last_scr += s->packet_size * 90000LL / (s->mux_rate * 50LL);
1075 while (stream->premux_packet &&
1076 stream->premux_packet->unwritten_size <= es_size) {
1077 es_size -= stream->premux_packet->unwritten_size;
1078 stream->premux_packet = stream->premux_packet->next;
1081 av_assert0(stream->premux_packet);
1082 stream->premux_packet->unwritten_size -= es_size;
1085 if (remove_decoded_packets(ctx, s->last_scr) < 0)
1091 static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
1093 int stream_index = pkt->stream_index;
1094 int size = pkt->size;
1095 uint8_t *buf = pkt->data;
1096 MpegMuxContext *s = ctx->priv_data;
1097 AVStream *st = ctx->streams[stream_index];
1098 StreamInfo *stream = st->priv_data;
1100 PacketDesc *pkt_desc;
1102 const int is_iframe = st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
1103 (pkt->flags & AV_PKT_FLAG_KEY);
1105 preload = av_rescale(s->preload, 90000, AV_TIME_BASE);
1110 if (s->last_scr == AV_NOPTS_VALUE) {
1111 if (dts == AV_NOPTS_VALUE || (dts < preload && ctx->avoid_negative_ts) || s->is_dvd) {
1112 if (dts != AV_NOPTS_VALUE)
1113 s->preload += av_rescale(-dts, AV_TIME_BASE, 90000);
1116 s->last_scr = dts - preload;
1119 preload = av_rescale(s->preload, 90000, AV_TIME_BASE);
1120 av_log(ctx, AV_LOG_DEBUG, "First SCR: %"PRId64" First DTS: %"PRId64"\n", s->last_scr, dts + preload);
1123 if (dts != AV_NOPTS_VALUE) dts += preload;
1124 if (pts != AV_NOPTS_VALUE) pts += preload;
1126 av_dlog(ctx, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n",
1127 dts / 90000.0, pts / 90000.0, pkt->flags,
1128 pkt->stream_index, pts != AV_NOPTS_VALUE);
1129 if (!stream->premux_packet)
1130 stream->next_packet = &stream->premux_packet;
1131 *stream->next_packet =
1132 pkt_desc = av_mallocz(sizeof(PacketDesc));
1133 pkt_desc->pts = pts;
1134 pkt_desc->dts = dts;
1135 pkt_desc->unwritten_size =
1136 pkt_desc->size = size;
1137 if (!stream->predecode_packet)
1138 stream->predecode_packet = pkt_desc;
1139 stream->next_packet = &pkt_desc->next;
1141 if (av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size) < 0)
1145 // min VOBU length 0.4 seconds (mpucoder)
1147 (s->packet_number == 0 ||
1148 (pts - stream->vobu_start_pts >= 36000))) {
1149 stream->bytes_to_iframe = av_fifo_size(stream->fifo);
1150 stream->align_iframe = 1;
1151 stream->vobu_start_pts = pts;
1155 av_fifo_generic_write(stream->fifo, buf, size, NULL);
1158 int ret = output_packet(ctx, 0);
1164 static int mpeg_mux_end(AVFormatContext *ctx)
1170 int ret = output_packet(ctx, 1);
1177 /* End header according to MPEG1 systems standard. We do not write
1178 * it as it is usually not needed by decoders and because it
1179 * complicates MPEG stream concatenation. */
1180 // avio_wb32(ctx->pb, ISO_11172_END_CODE);
1181 // avio_flush(ctx->pb);
1183 for (i = 0; i < ctx->nb_streams; i++) {
1184 stream = ctx->streams[i]->priv_data;
1186 av_assert0(av_fifo_size(stream->fifo) == 0);
1187 av_fifo_freep(&stream->fifo);
1192 #define OFFSET(x) offsetof(MpegMuxContext, x)
1193 #define E AV_OPT_FLAG_ENCODING_PARAM
1194 static const AVOption options[] = {
1195 { "muxrate", NULL, OFFSET(user_mux_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, ((1<<22) - 1) * (8 * 50), E },
1196 { "preload", "Initial demux-decode delay in microseconds.", OFFSET(preload), AV_OPT_TYPE_INT, { .i64 = 500000 }, 0, INT_MAX, E },
1200 #define MPEGENC_CLASS(flavor) \
1201 static const AVClass flavor ## _class = { \
1202 .class_name = #flavor " muxer", \
1203 .item_name = av_default_item_name, \
1204 .version = LIBAVUTIL_VERSION_INT, \
1205 .option = options, \
1208 #if CONFIG_MPEG1SYSTEM_MUXER
1210 AVOutputFormat ff_mpeg1system_muxer = {
1212 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream"),
1213 .mime_type = "video/mpeg",
1214 .extensions = "mpg,mpeg",
1215 .priv_data_size = sizeof(MpegMuxContext),
1216 .audio_codec = AV_CODEC_ID_MP2,
1217 .video_codec = AV_CODEC_ID_MPEG1VIDEO,
1218 .write_header = mpeg_mux_init,
1219 .write_packet = mpeg_mux_write_packet,
1220 .write_trailer = mpeg_mux_end,
1221 .priv_class = &mpeg_class,
1225 #if CONFIG_MPEG1VCD_MUXER
1227 AVOutputFormat ff_mpeg1vcd_muxer = {
1229 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream (VCD)"),
1230 .mime_type = "video/mpeg",
1231 .priv_data_size = sizeof(MpegMuxContext),
1232 .audio_codec = AV_CODEC_ID_MP2,
1233 .video_codec = AV_CODEC_ID_MPEG1VIDEO,
1234 .write_header = mpeg_mux_init,
1235 .write_packet = mpeg_mux_write_packet,
1236 .write_trailer = mpeg_mux_end,
1237 .priv_class = &vcd_class,
1241 #if CONFIG_MPEG2VOB_MUXER
1243 AVOutputFormat ff_mpeg2vob_muxer = {
1245 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (VOB)"),
1246 .mime_type = "video/mpeg",
1247 .extensions = "vob",
1248 .priv_data_size = sizeof(MpegMuxContext),
1249 .audio_codec = AV_CODEC_ID_MP2,
1250 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1251 .write_header = mpeg_mux_init,
1252 .write_packet = mpeg_mux_write_packet,
1253 .write_trailer = mpeg_mux_end,
1254 .priv_class = &vob_class,
1258 /* Same as mpeg2vob_mux except that the pack size is 2324 */
1259 #if CONFIG_MPEG2SVCD_MUXER
1261 AVOutputFormat ff_mpeg2svcd_muxer = {
1263 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (SVCD)"),
1264 .mime_type = "video/mpeg",
1265 .extensions = "vob",
1266 .priv_data_size = sizeof(MpegMuxContext),
1267 .audio_codec = AV_CODEC_ID_MP2,
1268 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1269 .write_header = mpeg_mux_init,
1270 .write_packet = mpeg_mux_write_packet,
1271 .write_trailer = mpeg_mux_end,
1272 .priv_class = &svcd_class,
1276 /* Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
1277 #if CONFIG_MPEG2DVD_MUXER
1279 AVOutputFormat ff_mpeg2dvd_muxer = {
1281 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (DVD VOB)"),
1282 .mime_type = "video/mpeg",
1283 .extensions = "dvd",
1284 .priv_data_size = sizeof(MpegMuxContext),
1285 .audio_codec = AV_CODEC_ID_MP2,
1286 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1287 .write_header = mpeg_mux_init,
1288 .write_packet = mpeg_mux_write_packet,
1289 .write_trailer = mpeg_mux_end,
1290 .priv_class = &dvd_class,