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
41 typedef struct PacketDesc {
47 struct PacketDesc *next;
53 int max_buffer_size; /* in bytes */
55 PacketDesc *predecode_packet;
56 PacketDesc *premux_packet;
57 PacketDesc **next_packet;
59 uint8_t lpcm_header[3];
63 int64_t vobu_start_pts;
68 int packet_size; /* required packet size */
70 int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
71 int system_header_freq;
72 int system_header_size;
73 int user_mux_rate; /* bitrate in units of bits/s */
74 int mux_rate; /* bitrate in units of 50 bytes/s */
82 int64_t last_scr; /* current system clock */
84 double vcd_padding_bitrate; // FIXME floats
85 int64_t vcd_padding_bytes_written;
90 extern AVOutputFormat ff_mpeg1vcd_muxer;
91 extern AVOutputFormat ff_mpeg2dvd_muxer;
92 extern AVOutputFormat ff_mpeg2svcd_muxer;
93 extern AVOutputFormat ff_mpeg2vob_muxer;
95 static int put_pack_header(AVFormatContext *ctx, uint8_t *buf,
98 MpegMuxContext *s = ctx->priv_data;
101 init_put_bits(&pb, buf, 128);
103 put_bits32(&pb, PACK_START_CODE);
105 put_bits(&pb, 2, 0x1);
107 put_bits(&pb, 4, 0x2);
108 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
110 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
112 put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
115 /* 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,
130 int only_for_stream_id)
132 MpegMuxContext *s = ctx->priv_data;
133 int size, i, private_stream_coded, id;
136 init_put_bits(&pb, buf, 128);
138 put_bits32(&pb, SYSTEM_HEADER_START_CODE);
139 put_bits(&pb, 16, 0);
142 /* maximum bit rate of the multiplexed stream */
143 put_bits(&pb, 22, s->mux_rate);
144 put_bits(&pb, 1, 1); /* marker */
145 if (s->is_vcd && only_for_stream_id == VIDEO_ID) {
146 /* This header applies only to the video stream
147 * (see VCD standard p. IV-7) */
150 put_bits(&pb, 6, s->audio_bound);
153 /* see VCD standard, p. IV-7 */
157 put_bits(&pb, 1, 0); /* variable bitrate */
158 put_bits(&pb, 1, 0); /* non constrainted bit stream */
161 if (s->is_vcd || s->is_dvd) {
162 /* see VCD standard p IV-7 */
163 put_bits(&pb, 1, 1); /* audio locked */
164 put_bits(&pb, 1, 1); /* video locked */
166 put_bits(&pb, 1, 0); /* audio locked */
167 put_bits(&pb, 1, 0); /* video locked */
170 put_bits(&pb, 1, 1); /* marker */
172 if (s->is_vcd && (only_for_stream_id & 0xe0) == AUDIO_ID) {
173 /* This header applies only to the audio stream
174 * (see VCD standard p. IV-7) */
177 put_bits(&pb, 5, s->video_bound);
180 put_bits(&pb, 1, 0); /* packet_rate_restriction_flag */
181 put_bits(&pb, 7, 0x7f); /* reserved byte */
183 put_bits(&pb, 8, 0xff); /* reserved byte */
185 /* DVD-Video Stream_bound entries
186 * id (0xB9) video, maximum P-STD for stream 0xE0. (P-STD_buffer_bound_scale = 1)
187 * 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)
188 * id (0xBD) private stream 1 (audio other than MPEG and subpictures). (P-STD_buffer_bound_scale = 1)
189 * id (0xBF) private stream 2, NAV packs, set to 2x1024. */
192 int P_STD_max_video = 0;
193 int P_STD_max_mpeg_audio = 0;
194 int P_STD_max_mpeg_PS1 = 0;
196 for (i = 0; i < ctx->nb_streams; i++) {
197 StreamInfo *stream = ctx->streams[i]->priv_data;
200 if (id == 0xbd && stream->max_buffer_size > P_STD_max_mpeg_PS1) {
201 P_STD_max_mpeg_PS1 = stream->max_buffer_size;
202 } else if (id >= 0xc0 && id <= 0xc7 &&
203 stream->max_buffer_size > P_STD_max_mpeg_audio) {
204 P_STD_max_mpeg_audio = stream->max_buffer_size;
205 } else if (id == 0xe0 &&
206 stream->max_buffer_size > P_STD_max_video) {
207 P_STD_max_video = stream->max_buffer_size;
212 put_bits(&pb, 8, 0xb9); /* stream ID */
215 put_bits(&pb, 13, P_STD_max_video / 1024);
218 if (P_STD_max_mpeg_audio == 0)
219 P_STD_max_mpeg_audio = 4096;
220 put_bits(&pb, 8, 0xb8); /* stream ID */
223 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
225 /* private stream 1 */
226 put_bits(&pb, 8, 0xbd); /* stream ID */
229 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
231 /* private stream 2 */
232 put_bits(&pb, 8, 0xbf); /* stream ID */
235 put_bits(&pb, 13, 2);
237 /* audio stream info */
238 private_stream_coded = 0;
239 for (i = 0; i < ctx->nb_streams; i++) {
240 StreamInfo *stream = ctx->streams[i]->priv_data;
242 /* For VCDs, only include the stream info for the stream
243 * that the pack which contains this system belongs to.
244 * (see VCD standard p. IV-7) */
245 if (!s->is_vcd || stream->id == only_for_stream_id ||
246 only_for_stream_id == 0) {
249 /* special case for private streams (AC-3 uses that) */
250 if (private_stream_coded)
252 private_stream_coded = 1;
255 put_bits(&pb, 8, id); /* stream ID */
260 put_bits(&pb, 13, stream->max_buffer_size / 128);
264 put_bits(&pb, 13, stream->max_buffer_size / 1024);
271 size = put_bits_ptr(&pb) - pb.buf;
272 /* patch packet size */
273 AV_WB16(buf + 4, size - 6);
278 static int get_system_header_size(AVFormatContext *ctx)
280 int buf_index, i, private_stream_coded;
282 MpegMuxContext *s = ctx->priv_data;
285 return 18; // DVD-Video system headers are 18 bytes fixed length.
288 private_stream_coded = 0;
289 for (i = 0; i < ctx->nb_streams; i++) {
290 stream = ctx->streams[i]->priv_data;
291 if (stream->id < 0xc0) {
292 if (private_stream_coded)
294 private_stream_coded = 1;
301 static av_cold int mpeg_mux_init(AVFormatContext *ctx)
303 MpegMuxContext *s = ctx->priv_data;
304 int bitrate, i, mpa_id, mpv_id, h264_id, mps_id, ac3_id, dts_id, lpcm_id, j;
310 s->packet_number = 0;
311 s->is_vcd = (CONFIG_MPEG1VCD_MUXER && ctx->oformat == &ff_mpeg1vcd_muxer);
312 s->is_svcd = (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer);
313 s->is_mpeg2 = ((CONFIG_MPEG2VOB_MUXER && ctx->oformat == &ff_mpeg2vob_muxer) ||
314 (CONFIG_MPEG2DVD_MUXER && ctx->oformat == &ff_mpeg2dvd_muxer) ||
315 (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer));
316 s->is_dvd = (CONFIG_MPEG2DVD_MUXER && ctx->oformat == &ff_mpeg2dvd_muxer);
318 if (ctx->packet_size) {
319 if (ctx->packet_size < 20 || ctx->packet_size > (1 << 23) + 10) {
320 av_log(ctx, AV_LOG_ERROR, "Invalid packet size %d\n",
324 s->packet_size = ctx->packet_size;
326 s->packet_size = 2048;
327 if (ctx->max_delay < 0) /* Not set by the caller */
328 ctx->max_delay = 0.7*AV_TIME_BASE;
330 s->vcd_padding_bytes_written = 0;
331 s->vcd_padding_bitrate = 0;
344 for (i = 0; i < ctx->nb_streams; i++) {
345 st = ctx->streams[i];
346 stream = av_mallocz(sizeof(StreamInfo));
349 st->priv_data = stream;
351 avpriv_set_pts_info(st, 64, 1, 90000);
353 switch (st->codec->codec_type) {
354 case AVMEDIA_TYPE_AUDIO:
356 (st->codec->codec_id == AV_CODEC_ID_AC3 ||
357 st->codec->codec_id == AV_CODEC_ID_DTS ||
358 st->codec->codec_id == AV_CODEC_ID_PCM_S16BE))
359 av_log(ctx, AV_LOG_WARNING,
360 "%s in MPEG-1 system streams is not widely supported, "
361 "consider using the vob or the dvd muxer "
362 "to force a MPEG-2 program stream.\n",
363 avcodec_get_name(st->codec->codec_id));
364 if (st->codec->codec_id == AV_CODEC_ID_AC3) {
365 stream->id = ac3_id++;
366 } else if (st->codec->codec_id == AV_CODEC_ID_DTS) {
367 stream->id = dts_id++;
368 } else if (st->codec->codec_id == AV_CODEC_ID_PCM_S16BE) {
369 stream->id = lpcm_id++;
370 for (j = 0; j < 4; j++) {
371 if (lpcm_freq_tab[j] == st->codec->sample_rate)
376 if (st->codec->channels > 8)
378 stream->lpcm_header[0] = 0x0c;
379 stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4);
380 stream->lpcm_header[2] = 0x80;
381 stream->lpcm_align = st->codec->channels * 2;
383 stream->id = mpa_id++;
386 /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
387 * Right now it is also used for everything else. */
388 stream->max_buffer_size = 4 * 1024;
391 case AVMEDIA_TYPE_VIDEO:
392 if (st->codec->codec_id == AV_CODEC_ID_H264)
393 stream->id = h264_id++;
395 stream->id = mpv_id++;
396 if (st->codec->rc_buffer_size)
397 stream->max_buffer_size = 6 * 1024 + st->codec->rc_buffer_size / 8;
399 av_log(ctx, AV_LOG_WARNING,
400 "VBV buffer size not set, using default size of 130KB\n"
401 "If you want the mpeg file to be compliant to some specification\n"
402 "Like DVD, VCD or others, make sure you set the correct buffer size\n");
403 // FIXME: this is probably too small as default
404 stream->max_buffer_size = 230 * 1024;
406 if (stream->max_buffer_size > 1024 * 8191) {
407 av_log(ctx, AV_LOG_WARNING, "buffer size %d, too large\n", stream->max_buffer_size);
408 stream->max_buffer_size = 1024 * 8191;
412 case AVMEDIA_TYPE_SUBTITLE:
413 stream->id = mps_id++;
414 stream->max_buffer_size = 16 * 1024;
419 stream->fifo = av_fifo_alloc(16);
426 for (i = 0; i < ctx->nb_streams; i++) {
428 st = ctx->streams[i];
429 stream = (StreamInfo *)st->priv_data;
431 if (st->codec->rc_max_rate ||
432 st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
433 codec_rate = st->codec->rc_max_rate;
435 codec_rate = st->codec->bit_rate;
438 codec_rate = (1 << 21) * 8 * 50 / ctx->nb_streams;
440 bitrate += codec_rate;
442 if ((stream->id & 0xe0) == AUDIO_ID)
443 audio_bitrate += codec_rate;
444 else if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
445 video_bitrate += codec_rate;
448 if (s->user_mux_rate) {
449 s->mux_rate = (s->user_mux_rate + (8 * 50) - 1) / (8 * 50);
451 /* we increase slightly the bitrate to take into account the
452 * headers. XXX: compute it exactly */
453 bitrate += bitrate / 20;
455 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
456 if (s->mux_rate >= (1<<22)) {
457 av_log(ctx, AV_LOG_WARNING, "mux rate %d is too large\n", s->mux_rate);
458 s->mux_rate = (1<<22) - 1;
463 double overhead_rate;
465 /* The VCD standard mandates that the mux_rate field is 3528
466 * (see standard p. IV-6).
467 * The value is actually "wrong", i.e. if you calculate
468 * it using the normal formula and the 75 sectors per second transfer
469 * rate you get a different value because the real pack size is 2324,
470 * not 2352. But the standard explicitly specifies that the mux_rate
471 * field in the header must have this value. */
472 // s->mux_rate = 2352 * 75 / 50; /* = 3528 */
474 /* The VCD standard states that the muxed stream must be
475 * exactly 75 packs / second (the data rate of a single speed cdrom).
476 * Since the video bitrate (probably 1150000 bits/sec) will be below
477 * the theoretical maximum we have to add some padding packets
478 * to make up for the lower data rate.
479 * (cf. VCD standard p. IV-6 ) */
481 /* Add the header overhead to the data rate.
482 * 2279 data bytes per audio pack, 2294 data bytes per video pack */
483 overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
484 overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
487 /* Add padding so that the full bitrate is 2324*75 bytes/sec */
488 s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate);
491 if (s->is_vcd || s->is_mpeg2)
493 s->pack_header_freq = 1;
495 /* every 2 seconds */
496 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
498 /* the above seems to make pack_header_freq zero sometimes */
499 if (s->pack_header_freq == 0)
500 s->pack_header_freq = 1;
503 /* every 200 packets. Need to look at the spec. */
504 s->system_header_freq = s->pack_header_freq * 40;
506 /* the standard mandates that there are only two system headers
507 * in the whole file: one in the first packet of each stream.
508 * (see standard p. IV-7 and IV-8) */
509 s->system_header_freq = 0x7fffffff;
511 s->system_header_freq = s->pack_header_freq * 5;
513 for (i = 0; i < ctx->nb_streams; i++) {
514 stream = ctx->streams[i]->priv_data;
515 stream->packet_number = 0;
517 s->system_header_size = get_system_header_size(ctx);
518 s->last_scr = AV_NOPTS_VALUE;
522 for (i = 0; i < ctx->nb_streams; i++)
523 av_free(ctx->streams[i]->priv_data);
524 return AVERROR(ENOMEM);
527 static inline void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
529 avio_w8(pb, (id << 4) | (((timestamp >> 30) & 0x07) << 1) | 1);
530 avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
531 avio_wb16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
534 /* return the number of padding bytes that should be inserted into
535 * the multiplexed stream. */
536 static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
538 MpegMuxContext *s = ctx->priv_data;
541 if (s->vcd_padding_bitrate > 0 && pts != AV_NOPTS_VALUE) {
542 int64_t full_pad_bytes;
544 // FIXME: this is wrong
546 (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0);
547 pad_bytes = (int)(full_pad_bytes - s->vcd_padding_bytes_written);
550 /* might happen if we have already padded to a later timestamp. This
551 * can occur if another stream has already advanced further. */
558 /* Write an MPEG padding packet header. */
559 static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb,
562 MpegMuxContext *s = ctx->priv_data;
565 avio_wb32(pb, PADDING_STREAM);
566 avio_wb16(pb, packet_bytes - 6);
573 for (i = 0; i < packet_bytes; i++)
577 static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len)
580 PacketDesc *pkt_desc = stream->premux_packet;
583 if (pkt_desc->size == pkt_desc->unwritten_size)
585 len -= pkt_desc->unwritten_size;
586 pkt_desc = pkt_desc->next;
592 /* flush the packet on stream stream_index */
593 static int flush_packet(AVFormatContext *ctx, int stream_index,
594 int64_t pts, int64_t dts, int64_t scr, int trailer_size)
596 MpegMuxContext *s = ctx->priv_data;
597 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
599 int size, payload_size, startcode, id, stuffing_size, i, header_len;
602 int zero_trail_bytes = 0;
603 int pad_packet_bytes = 0;
605 /* "general" pack without data specific to one stream? */
606 int general_pack = 0;
611 av_dlog(ctx, "packet ID=%2x PTS=%0.3f\n", id, pts / 90000.0);
615 if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) {
616 /* output pack and systems header if needed */
617 size = put_pack_header(ctx, buf_ptr, scr);
622 /* there is exactly one system header for each stream in a VCD MPEG,
623 * One in the very first video packet and one in the very first
624 * audio packet (see VCD standard p. IV-7 and IV-8). */
626 if (stream->packet_number == 0) {
627 size = put_system_header(ctx, buf_ptr, id);
630 } else if (s->is_dvd) {
631 if (stream->align_iframe || s->packet_number == 0) {
632 int PES_bytes_to_fill = s->packet_size - size - 10;
634 if (pts != AV_NOPTS_VALUE) {
636 PES_bytes_to_fill -= 5 + 5;
638 PES_bytes_to_fill -= 5;
641 if (stream->bytes_to_iframe == 0 || s->packet_number == 0) {
642 size = put_system_header(ctx, buf_ptr, 0);
644 size = buf_ptr - buffer;
645 avio_write(ctx->pb, buffer, size);
647 avio_wb32(ctx->pb, PRIVATE_STREAM_2);
648 avio_wb16(ctx->pb, 0x03d4); // length
649 avio_w8(ctx->pb, 0x00); // substream ID, 00=PCI
650 for (i = 0; i < 979; i++)
651 avio_w8(ctx->pb, 0x00);
653 avio_wb32(ctx->pb, PRIVATE_STREAM_2);
654 avio_wb16(ctx->pb, 0x03fa); // length
655 avio_w8(ctx->pb, 0x01); // substream ID, 01=DSI
656 for (i = 0; i < 1017; i++)
657 avio_w8(ctx->pb, 0x00);
659 memset(buffer, 0, 128);
662 stream->align_iframe = 0;
663 // FIXME: rounding and first few bytes of each packet
664 scr += s->packet_size * 90000LL /
665 (s->mux_rate * 50LL);
666 size = put_pack_header(ctx, buf_ptr, scr);
670 } else if (stream->bytes_to_iframe < PES_bytes_to_fill) {
671 pad_packet_bytes = PES_bytes_to_fill -
672 stream->bytes_to_iframe;
676 if ((s->packet_number % s->system_header_freq) == 0) {
677 size = put_system_header(ctx, buf_ptr, 0);
682 size = buf_ptr - buffer;
683 avio_write(ctx->pb, buffer, size);
685 packet_size = s->packet_size - size;
687 if (s->is_vcd && (id & 0xe0) == AUDIO_ID)
688 /* The VCD standard demands that 20 zero bytes follow
689 * each audio pack (see standard p. IV-8). */
690 zero_trail_bytes += 20;
692 if ((s->is_vcd && stream->packet_number == 0) ||
693 (s->is_svcd && s->packet_number == 0)) {
694 /* for VCD the first pack of each stream contains only the pack header,
695 * the system header and lots of padding (see VCD standard p. IV-6).
696 * In the case of an audio pack, 20 zero bytes are also added at
698 /* For SVCD we fill the very first pack to increase compatibility with
699 * some DVD players. Not mandated by the standard. */
701 /* the system header refers to both streams and no stream data */
703 pad_packet_bytes = packet_size - zero_trail_bytes;
706 packet_size -= pad_packet_bytes + zero_trail_bytes;
708 if (packet_size > 0) {
709 /* packet header size */
715 if (stream->packet_number == 0)
716 header_len += 3; /* PES extension */
717 header_len += 1; /* obligatory stuffing byte */
721 if (pts != AV_NOPTS_VALUE) {
731 payload_size = packet_size - header_len;
733 startcode = PRIVATE_STREAM_1;
741 startcode = 0x100 + id;
744 stuffing_size = payload_size - av_fifo_size(stream->fifo);
746 // first byte does not fit -> reset pts/dts + stuffing
747 if (payload_size <= trailer_size && pts != AV_NOPTS_VALUE) {
748 int timestamp_len = 0;
751 if (pts != AV_NOPTS_VALUE)
752 timestamp_len += s->is_mpeg2 ? 5 : 4;
754 dts = AV_NOPTS_VALUE;
755 header_len -= timestamp_len;
756 if (s->is_dvd && stream->align_iframe) {
757 pad_packet_bytes += timestamp_len;
758 packet_size -= timestamp_len;
760 payload_size += timestamp_len;
762 stuffing_size += timestamp_len;
763 if (payload_size > trailer_size)
764 stuffing_size += payload_size - trailer_size;
767 // can't use padding, so use stuffing
768 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) {
769 packet_size += pad_packet_bytes;
770 payload_size += pad_packet_bytes; // undo the previous adjustment
771 if (stuffing_size < 0)
772 stuffing_size = pad_packet_bytes;
774 stuffing_size += pad_packet_bytes;
775 pad_packet_bytes = 0;
778 if (stuffing_size < 0)
781 if (startcode == PRIVATE_STREAM_1 && id >= 0xa0) {
782 if (payload_size < av_fifo_size(stream->fifo))
783 stuffing_size += payload_size % stream->lpcm_align;
786 if (stuffing_size > 16) { /* <=16 for MPEG-1, <=32 for MPEG-2 */
787 pad_packet_bytes += stuffing_size;
788 packet_size -= stuffing_size;
789 payload_size -= stuffing_size;
793 nb_frames = get_nb_frames(ctx, stream, payload_size - stuffing_size);
795 avio_wb32(ctx->pb, startcode);
797 avio_wb16(ctx->pb, packet_size);
800 for (i = 0; i < stuffing_size; i++)
801 avio_w8(ctx->pb, 0xff);
804 avio_w8(ctx->pb, 0x80); /* mpeg2 id */
808 if (pts != AV_NOPTS_VALUE) {
814 /* Both the MPEG-2 and the SVCD standards demand that the
815 * P-STD_buffer_size field be included in the first packet of
816 * every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
817 * and MPEG-2 standard 2.7.7) */
818 if (stream->packet_number == 0)
821 avio_w8(ctx->pb, pes_flags); /* flags */
822 avio_w8(ctx->pb, header_len - 3 + stuffing_size);
824 if (pes_flags & 0x80) /* write pts */
825 put_timestamp(ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
826 if (pes_flags & 0x40) /* write dts */
827 put_timestamp(ctx->pb, 0x01, dts);
829 if (pes_flags & 0x01) { /* write pes extension */
830 avio_w8(ctx->pb, 0x10); /* flags */
832 /* P-STD buffer info */
833 if ((id & 0xe0) == AUDIO_ID)
834 avio_wb16(ctx->pb, 0x4000 | stream->max_buffer_size / 128);
836 avio_wb16(ctx->pb, 0x6000 | stream->max_buffer_size / 1024);
839 if (pts != AV_NOPTS_VALUE) {
841 put_timestamp(ctx->pb, 0x03, pts);
842 put_timestamp(ctx->pb, 0x01, dts);
844 put_timestamp(ctx->pb, 0x02, pts);
847 avio_w8(ctx->pb, 0x0f);
852 /* special stuffing byte that is always written
853 * to prevent accidental generation of start codes. */
854 avio_w8(ctx->pb, 0xff);
856 for (i = 0; i < stuffing_size; i++)
857 avio_w8(ctx->pb, 0xff);
860 if (startcode == PRIVATE_STREAM_1) {
861 avio_w8(ctx->pb, id);
863 /* LPCM (XXX: check nb_frames) */
865 avio_wb16(ctx->pb, 4); /* skip 3 header bytes */
866 avio_w8(ctx->pb, stream->lpcm_header[0]);
867 avio_w8(ctx->pb, stream->lpcm_header[1]);
868 avio_w8(ctx->pb, stream->lpcm_header[2]);
869 } else if (id >= 0x40) {
871 avio_w8(ctx->pb, nb_frames);
872 avio_wb16(ctx->pb, trailer_size + 1);
877 assert(payload_size - stuffing_size <= av_fifo_size(stream->fifo));
878 av_fifo_generic_read(stream->fifo, ctx->pb,
879 payload_size - stuffing_size,
880 (void (*)(void*, void*, int))avio_write);
881 stream->bytes_to_iframe -= payload_size - stuffing_size;
887 if (pad_packet_bytes > 0)
888 put_padding_packet(ctx, ctx->pb, pad_packet_bytes);
890 for (i = 0; i < zero_trail_bytes; i++)
891 avio_w8(ctx->pb, 0x00);
897 /* only increase the stream packet number if this pack actually contains
898 * something that is specific to this stream! I.e. a dedicated header
901 stream->packet_number++;
903 return payload_size - stuffing_size;
906 static void put_vcd_padding_sector(AVFormatContext *ctx)
908 /* There are two ways to do this padding: writing a sector/pack
909 * of 0 values, or writing an MPEG padding pack. Both seem to
910 * work with most decoders, BUT the VCD standard only allows a 0-sector
911 * (see standard p. IV-4, IV-5).
912 * So a 0-sector it is... */
914 MpegMuxContext *s = ctx->priv_data;
917 for (i = 0; i < s->packet_size; i++)
920 s->vcd_padding_bytes_written += s->packet_size;
924 /* increasing the packet number is correct. The SCR of the following packs
925 * is calculated from the packet_number and it has to include the padding
926 * sector (it represents the sector index, not the MPEG pack index)
927 * (see VCD standard p. IV-6) */
931 static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr)
935 for (i = 0; i < ctx->nb_streams; i++) {
936 AVStream *st = ctx->streams[i];
937 StreamInfo *stream = st->priv_data;
938 PacketDesc *pkt_desc;
940 while ((pkt_desc = stream->predecode_packet) &&
941 scr > pkt_desc->dts) { // FIXME: > vs >=
942 if (stream->buffer_index < pkt_desc->size ||
943 stream->predecode_packet == stream->premux_packet) {
944 av_log(ctx, AV_LOG_ERROR,
945 "buffer underflow st=%d bufi=%d size=%d\n",
946 i, stream->buffer_index, pkt_desc->size);
949 stream->buffer_index -= pkt_desc->size;
950 stream->predecode_packet = pkt_desc->next;
958 static int output_packet(AVFormatContext *ctx, int flush)
960 MpegMuxContext *s = ctx->priv_data;
963 int i, avail_space = 0, es_size, trailer_size;
965 int best_score = INT_MIN;
966 int ignore_constraints = 0;
967 int64_t scr = s->last_scr;
968 PacketDesc *timestamp_packet;
969 const int64_t max_delay = av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
972 for (i = 0; i < ctx->nb_streams; i++) {
973 AVStream *st = ctx->streams[i];
974 StreamInfo *stream = st->priv_data;
975 const int avail_data = av_fifo_size(stream->fifo);
976 const int space = stream->max_buffer_size - stream->buffer_index;
977 int rel_space = 1024LL * space / stream->max_buffer_size;
978 PacketDesc *next_pkt = stream->premux_packet;
980 /* for subtitle, a single PES packet must be generated,
981 * so we flush after every single subtitle packet */
982 if (s->packet_size > avail_data && !flush
983 && st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE)
987 av_assert0(avail_data > 0);
989 if (space < s->packet_size && !ignore_constraints)
992 if (next_pkt && next_pkt->dts - scr > max_delay)
994 if ( stream->predecode_packet
995 && stream->predecode_packet->size > stream->buffer_index)
997 if (rel_space > best_score) {
998 best_score = rel_space;
1000 avail_space = space;
1005 int64_t best_dts = INT64_MAX;
1007 for (i = 0; i < ctx->nb_streams; i++) {
1008 AVStream *st = ctx->streams[i];
1009 StreamInfo *stream = st->priv_data;
1010 PacketDesc *pkt_desc = stream->predecode_packet;
1011 if (pkt_desc && pkt_desc->dts < best_dts)
1012 best_dts = pkt_desc->dts;
1015 av_dlog(ctx, "bumping scr, scr:%f, dts:%f\n",
1016 scr / 90000.0, best_dts / 90000.0);
1017 if (best_dts == INT64_MAX)
1020 if (scr >= best_dts + 1 && !ignore_constraints) {
1021 av_log(ctx, AV_LOG_ERROR,
1022 "packet too large, ignoring buffer limits to mux it\n");
1023 ignore_constraints = 1;
1025 scr = FFMAX(best_dts + 1, scr);
1026 if (remove_decoded_packets(ctx, scr) < 0)
1031 assert(best_i >= 0);
1033 st = ctx->streams[best_i];
1034 stream = st->priv_data;
1036 assert(av_fifo_size(stream->fifo) > 0);
1038 assert(avail_space >= s->packet_size || ignore_constraints);
1040 timestamp_packet = stream->premux_packet;
1041 if (timestamp_packet->unwritten_size == timestamp_packet->size) {
1044 trailer_size = timestamp_packet->unwritten_size;
1045 timestamp_packet = timestamp_packet->next;
1048 if (timestamp_packet) {
1049 av_dlog(ctx, "dts:%f pts:%f scr:%f stream:%d\n",
1050 timestamp_packet->dts / 90000.0,
1051 timestamp_packet->pts / 90000.0,
1052 scr / 90000.0, best_i);
1053 es_size = flush_packet(ctx, best_i, timestamp_packet->pts,
1054 timestamp_packet->dts, scr, trailer_size);
1056 assert(av_fifo_size(stream->fifo) == trailer_size);
1057 es_size = flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr,
1062 /* Write one or more padding sectors, if necessary, to reach
1063 * the constant overall bitrate. */
1066 // FIXME: pts cannot be correct here
1067 while ((vcd_pad_bytes = get_vcd_padding_size(ctx, stream->premux_packet->pts)) >= s->packet_size) {
1068 put_vcd_padding_sector(ctx);
1069 // FIXME: rounding and first few bytes of each packet
1070 s->last_scr += s->packet_size * 90000LL / (s->mux_rate * 50LL);
1074 stream->buffer_index += es_size;
1075 // FIXME: rounding and first few bytes of each packet
1076 s->last_scr += s->packet_size * 90000LL / (s->mux_rate * 50LL);
1078 while (stream->premux_packet &&
1079 stream->premux_packet->unwritten_size <= es_size) {
1080 es_size -= stream->premux_packet->unwritten_size;
1081 stream->premux_packet = stream->premux_packet->next;
1084 stream->premux_packet->unwritten_size -= es_size;
1086 if (remove_decoded_packets(ctx, s->last_scr) < 0)
1092 static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
1094 int stream_index = pkt->stream_index;
1095 int size = pkt->size;
1096 uint8_t *buf = pkt->data;
1097 MpegMuxContext *s = ctx->priv_data;
1098 AVStream *st = ctx->streams[stream_index];
1099 StreamInfo *stream = st->priv_data;
1101 PacketDesc *pkt_desc;
1103 const int is_iframe = st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
1104 (pkt->flags & AV_PKT_FLAG_KEY);
1106 preload = av_rescale(s->preload, 90000, AV_TIME_BASE);
1111 if (s->last_scr == AV_NOPTS_VALUE) {
1112 if (dts == AV_NOPTS_VALUE || (dts < preload && ctx->avoid_negative_ts) || s->is_dvd) {
1113 if (dts != AV_NOPTS_VALUE)
1114 s->preload += av_rescale(-dts, AV_TIME_BASE, 90000);
1117 s->last_scr = dts - preload;
1120 preload = av_rescale(s->preload, 90000, AV_TIME_BASE);
1121 av_log(ctx, AV_LOG_DEBUG, "First SCR: %"PRId64" First DTS: %"PRId64"\n", s->last_scr, dts + preload);
1124 if (dts != AV_NOPTS_VALUE) dts += preload;
1125 if (pts != AV_NOPTS_VALUE) pts += preload;
1127 av_dlog(ctx, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n",
1128 dts / 90000.0, pts / 90000.0, pkt->flags,
1129 pkt->stream_index, pts != AV_NOPTS_VALUE);
1130 if (!stream->premux_packet)
1131 stream->next_packet = &stream->premux_packet;
1132 *stream->next_packet =
1133 pkt_desc = av_mallocz(sizeof(PacketDesc));
1134 pkt_desc->pts = pts;
1135 pkt_desc->dts = dts;
1136 pkt_desc->unwritten_size =
1137 pkt_desc->size = size;
1138 if (!stream->predecode_packet)
1139 stream->predecode_packet = pkt_desc;
1140 stream->next_packet = &pkt_desc->next;
1142 if (av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size) < 0)
1146 // min VOBU length 0.4 seconds (mpucoder)
1148 (s->packet_number == 0 ||
1149 (pts - stream->vobu_start_pts >= 36000))) {
1150 stream->bytes_to_iframe = av_fifo_size(stream->fifo);
1151 stream->align_iframe = 1;
1152 stream->vobu_start_pts = pts;
1156 av_fifo_generic_write(stream->fifo, buf, size, NULL);
1159 int ret = output_packet(ctx, 0);
1165 static int mpeg_mux_end(AVFormatContext *ctx)
1171 int ret = output_packet(ctx, 1);
1178 /* End header according to MPEG1 systems standard. We do not write
1179 * it as it is usually not needed by decoders and because it
1180 * complicates MPEG stream concatenation. */
1181 // avio_wb32(ctx->pb, ISO_11172_END_CODE);
1182 // avio_flush(ctx->pb);
1184 for (i = 0; i < ctx->nb_streams; i++) {
1185 stream = ctx->streams[i]->priv_data;
1187 assert(av_fifo_size(stream->fifo) == 0);
1188 av_fifo_freep(&stream->fifo);
1193 #define OFFSET(x) offsetof(MpegMuxContext, x)
1194 #define E AV_OPT_FLAG_ENCODING_PARAM
1195 static const AVOption options[] = {
1196 { "muxrate", NULL, OFFSET(user_mux_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, ((1<<22) - 1) * (8 * 50), E },
1197 { "preload", "Initial demux-decode delay in microseconds.", OFFSET(preload), AV_OPT_TYPE_INT, { .i64 = 500000 }, 0, INT_MAX, E },
1201 #define MPEGENC_CLASS(flavor) \
1202 static const AVClass flavor ## _class = { \
1203 .class_name = #flavor " muxer", \
1204 .item_name = av_default_item_name, \
1205 .version = LIBAVUTIL_VERSION_INT, \
1206 .option = options, \
1209 #if CONFIG_MPEG1SYSTEM_MUXER
1211 AVOutputFormat ff_mpeg1system_muxer = {
1213 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream"),
1214 .mime_type = "video/mpeg",
1215 .extensions = "mpg,mpeg",
1216 .priv_data_size = sizeof(MpegMuxContext),
1217 .audio_codec = AV_CODEC_ID_MP2,
1218 .video_codec = AV_CODEC_ID_MPEG1VIDEO,
1219 .write_header = mpeg_mux_init,
1220 .write_packet = mpeg_mux_write_packet,
1221 .write_trailer = mpeg_mux_end,
1222 .priv_class = &mpeg_class,
1226 #if CONFIG_MPEG1VCD_MUXER
1228 AVOutputFormat ff_mpeg1vcd_muxer = {
1230 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream (VCD)"),
1231 .mime_type = "video/mpeg",
1232 .priv_data_size = sizeof(MpegMuxContext),
1233 .audio_codec = AV_CODEC_ID_MP2,
1234 .video_codec = AV_CODEC_ID_MPEG1VIDEO,
1235 .write_header = mpeg_mux_init,
1236 .write_packet = mpeg_mux_write_packet,
1237 .write_trailer = mpeg_mux_end,
1238 .priv_class = &vcd_class,
1242 #if CONFIG_MPEG2VOB_MUXER
1244 AVOutputFormat ff_mpeg2vob_muxer = {
1246 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (VOB)"),
1247 .mime_type = "video/mpeg",
1248 .extensions = "vob",
1249 .priv_data_size = sizeof(MpegMuxContext),
1250 .audio_codec = AV_CODEC_ID_MP2,
1251 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1252 .write_header = mpeg_mux_init,
1253 .write_packet = mpeg_mux_write_packet,
1254 .write_trailer = mpeg_mux_end,
1255 .priv_class = &vob_class,
1259 /* Same as mpeg2vob_mux except that the pack size is 2324 */
1260 #if CONFIG_MPEG2SVCD_MUXER
1262 AVOutputFormat ff_mpeg2svcd_muxer = {
1264 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (SVCD)"),
1265 .mime_type = "video/mpeg",
1266 .extensions = "vob",
1267 .priv_data_size = sizeof(MpegMuxContext),
1268 .audio_codec = AV_CODEC_ID_MP2,
1269 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1270 .write_header = mpeg_mux_init,
1271 .write_packet = mpeg_mux_write_packet,
1272 .write_trailer = mpeg_mux_end,
1273 .priv_class = &svcd_class,
1277 /* Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
1278 #if CONFIG_MPEG2DVD_MUXER
1280 AVOutputFormat ff_mpeg2dvd_muxer = {
1282 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (DVD VOB)"),
1283 .mime_type = "video/mpeg",
1284 .extensions = "dvd",
1285 .priv_data_size = sizeof(MpegMuxContext),
1286 .audio_codec = AV_CODEC_ID_MP2,
1287 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1288 .write_header = mpeg_mux_init,
1289 .write_packet = mpeg_mux_write_packet,
1290 .write_trailer = mpeg_mux_end,
1291 .priv_class = &dvd_class,