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 {
43 struct PacketDesc *next;
46 typedef struct StreamInfo {
49 int max_buffer_size; /* in bytes */
51 PacketDesc *predecode_packet;
52 PacketDesc *premux_packet;
53 PacketDesc **next_packet;
55 uint8_t lpcm_header[3];
59 int64_t vobu_start_pts;
62 typedef struct MpegMuxContext {
64 int packet_size; /* required packet size */
66 int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
67 int system_header_freq;
68 int system_header_size;
69 int user_mux_rate; /* bitrate in units of bits/s */
70 int mux_rate; /* bitrate in units of 50 bytes/s */
78 int64_t last_scr; /* current system clock */
80 int64_t vcd_padding_bitrate_num;
81 int64_t vcd_padding_bytes_written;
86 extern AVOutputFormat ff_mpeg1vcd_muxer;
87 extern AVOutputFormat ff_mpeg2dvd_muxer;
88 extern AVOutputFormat ff_mpeg2svcd_muxer;
89 extern AVOutputFormat ff_mpeg2vob_muxer;
91 static int put_pack_header(AVFormatContext *ctx, uint8_t *buf,
94 MpegMuxContext *s = ctx->priv_data;
97 init_put_bits(&pb, buf, 128);
99 put_bits32(&pb, PACK_START_CODE);
101 put_bits(&pb, 2, 0x1);
103 put_bits(&pb, 4, 0x2);
104 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
106 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
108 put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
111 /* clock extension */
114 put_bits(&pb, 22, s->mux_rate);
118 put_bits(&pb, 5, 0x1f); /* reserved */
119 put_bits(&pb, 3, 0); /* stuffing length */
122 return put_bits_ptr(&pb) - pb.buf;
125 static int put_system_header(AVFormatContext *ctx, uint8_t *buf,
126 int only_for_stream_id)
128 MpegMuxContext *s = ctx->priv_data;
129 int size, i, private_stream_coded, id;
132 init_put_bits(&pb, buf, 128);
134 put_bits32(&pb, SYSTEM_HEADER_START_CODE);
135 put_bits(&pb, 16, 0);
138 /* maximum bit rate of the multiplexed stream */
139 put_bits(&pb, 22, s->mux_rate);
140 put_bits(&pb, 1, 1); /* marker */
141 if (s->is_vcd && only_for_stream_id == VIDEO_ID) {
142 /* This header applies only to the video stream
143 * (see VCD standard p. IV-7) */
146 put_bits(&pb, 6, s->audio_bound);
149 /* see VCD standard, p. IV-7 */
153 put_bits(&pb, 1, 0); /* variable bitrate */
154 put_bits(&pb, 1, 0); /* nonconstrained bitstream */
157 if (s->is_vcd || s->is_dvd) {
158 /* see VCD standard p IV-7 */
159 put_bits(&pb, 1, 1); /* audio locked */
160 put_bits(&pb, 1, 1); /* video locked */
162 put_bits(&pb, 1, 0); /* audio locked */
163 put_bits(&pb, 1, 0); /* video locked */
166 put_bits(&pb, 1, 1); /* marker */
168 if (s->is_vcd && (only_for_stream_id & 0xe0) == AUDIO_ID) {
169 /* This header applies only to the audio stream
170 * (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 &&
199 stream->max_buffer_size > P_STD_max_mpeg_audio) {
200 P_STD_max_mpeg_audio = stream->max_buffer_size;
201 } else if (id == 0xe0 &&
202 stream->max_buffer_size > P_STD_max_video) {
203 P_STD_max_video = stream->max_buffer_size;
208 put_bits(&pb, 8, 0xb9); /* stream ID */
211 put_bits(&pb, 13, P_STD_max_video / 1024);
214 if (P_STD_max_mpeg_audio == 0)
215 P_STD_max_mpeg_audio = 4096;
216 put_bits(&pb, 8, 0xb8); /* stream ID */
219 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
221 /* private stream 1 */
222 put_bits(&pb, 8, 0xbd); /* stream ID */
225 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
227 /* private stream 2 */
228 put_bits(&pb, 8, 0xbf); /* stream ID */
231 put_bits(&pb, 13, 2);
233 /* audio stream info */
234 private_stream_coded = 0;
235 for (i = 0; i < ctx->nb_streams; i++) {
236 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) {
245 /* special case for private streams (AC-3 uses that) */
246 if (private_stream_coded)
248 private_stream_coded = 1;
251 put_bits(&pb, 8, id); /* stream ID */
256 put_bits(&pb, 13, stream->max_buffer_size / 128);
260 put_bits(&pb, 13, stream->max_buffer_size / 1024);
267 size = put_bits_ptr(&pb) - pb.buf;
268 /* patch packet size */
269 AV_WB16(buf + 4, size - 6);
274 static int get_system_header_size(AVFormatContext *ctx)
276 int buf_index, i, private_stream_coded;
278 MpegMuxContext *s = ctx->priv_data;
281 return 18; // DVD-Video system headers are 18 bytes fixed length.
284 private_stream_coded = 0;
285 for (i = 0; i < ctx->nb_streams; i++) {
286 stream = ctx->streams[i]->priv_data;
287 if (stream->id < 0xc0) {
288 if (private_stream_coded)
290 private_stream_coded = 1;
297 static av_cold int mpeg_mux_init(AVFormatContext *ctx)
299 MpegMuxContext *s = ctx->priv_data;
300 int bitrate, i, mpa_id, mpv_id, h264_id, mps_id, ac3_id, dts_id, lpcm_id, j;
306 s->packet_number = 0;
307 s->is_vcd = (CONFIG_MPEG1VCD_MUXER && ctx->oformat == &ff_mpeg1vcd_muxer);
308 s->is_svcd = (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer);
309 s->is_mpeg2 = ((CONFIG_MPEG2VOB_MUXER && ctx->oformat == &ff_mpeg2vob_muxer) ||
310 (CONFIG_MPEG2DVD_MUXER && ctx->oformat == &ff_mpeg2dvd_muxer) ||
311 (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer));
312 s->is_dvd = (CONFIG_MPEG2DVD_MUXER && ctx->oformat == &ff_mpeg2dvd_muxer);
314 if (ctx->packet_size) {
315 if (ctx->packet_size < 20 || ctx->packet_size > (1 << 23) + 10) {
316 av_log(ctx, AV_LOG_ERROR, "Invalid packet size %d\n",
320 s->packet_size = ctx->packet_size;
322 s->packet_size = 2048;
323 if (ctx->max_delay < 0) /* Not set by the caller */
324 ctx->max_delay = AV_TIME_BASE*7/10;
326 s->vcd_padding_bytes_written = 0;
327 s->vcd_padding_bitrate_num = 0;
340 for (i = 0; i < ctx->nb_streams; i++) {
341 AVCPBProperties *props;
343 st = ctx->streams[i];
344 stream = av_mallocz(sizeof(StreamInfo));
347 st->priv_data = stream;
349 avpriv_set_pts_info(st, 64, 1, 90000);
351 switch (st->codecpar->codec_type) {
352 case AVMEDIA_TYPE_AUDIO:
354 (st->codecpar->codec_id == AV_CODEC_ID_AC3 ||
355 st->codecpar->codec_id == AV_CODEC_ID_DTS ||
356 st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE ||
357 st->codecpar->codec_id == AV_CODEC_ID_PCM_DVD))
358 av_log(ctx, AV_LOG_WARNING,
359 "%s in MPEG-1 system streams is not widely supported, "
360 "consider using the vob or the dvd muxer "
361 "to force a MPEG-2 program stream.\n",
362 avcodec_get_name(st->codecpar->codec_id));
363 if (st->codecpar->codec_id == AV_CODEC_ID_AC3) {
364 stream->id = ac3_id++;
365 } else if (st->codecpar->codec_id == AV_CODEC_ID_DTS) {
366 stream->id = dts_id++;
367 } else if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S16BE) {
368 stream->id = lpcm_id++;
369 for (j = 0; j < 4; j++) {
370 if (lpcm_freq_tab[j] == st->codecpar->sample_rate)
375 av_log(ctx, AV_LOG_ERROR, "Invalid sampling rate for PCM stream.\n");
376 av_log(ctx, AV_LOG_INFO, "Allowed sampling rates:");
377 for (sr = 0; sr < 4; sr++)
378 av_log(ctx, AV_LOG_INFO, " %d", lpcm_freq_tab[sr]);
379 av_log(ctx, AV_LOG_INFO, "\n");
382 if (st->codecpar->channels > 8) {
383 av_log(ctx, AV_LOG_ERROR, "At most 8 channels allowed for LPCM streams.\n");
386 stream->lpcm_header[0] = 0x0c;
387 stream->lpcm_header[1] = (st->codecpar->channels - 1) | (j << 4);
388 stream->lpcm_header[2] = 0x80;
389 stream->lpcm_align = st->codecpar->channels * 2;
390 } else if (st->codecpar->codec_id == AV_CODEC_ID_PCM_DVD) {
393 switch (st->codecpar->sample_rate) {
394 case 48000: freq = 0; break;
395 case 96000: freq = 1; break;
396 case 44100: freq = 2; break;
397 case 32000: freq = 3; break;
399 av_log(ctx, AV_LOG_ERROR, "Unsupported sample rate.\n");
400 return AVERROR(EINVAL);
403 stream->lpcm_header[0] = 0x0c;
404 stream->lpcm_header[1] = (freq << 4) |
405 (((st->codecpar->bits_per_coded_sample - 16) / 4) << 6) |
406 st->codecpar->channels - 1;
407 stream->lpcm_header[2] = 0x80;
408 stream->id = lpcm_id++;
409 stream->lpcm_align = st->codecpar->channels * st->codecpar->bits_per_coded_sample / 8;
411 stream->id = mpa_id++;
414 /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
415 * Right now it is also used for everything else. */
416 stream->max_buffer_size = 4 * 1024;
419 case AVMEDIA_TYPE_VIDEO:
420 if (st->codecpar->codec_id == AV_CODEC_ID_H264)
421 stream->id = h264_id++;
423 stream->id = mpv_id++;
425 props = (AVCPBProperties*)av_stream_get_side_data(st, AV_PKT_DATA_CPB_PROPERTIES, NULL);
426 if (props && props->buffer_size)
427 stream->max_buffer_size = 6 * 1024 + props->buffer_size / 8;
429 av_log(ctx, AV_LOG_WARNING,
430 "VBV buffer size not set, using default size of 230KB\n"
431 "If you want the mpeg file to be compliant to some specification\n"
432 "Like DVD, VCD or others, make sure you set the correct buffer size\n");
433 // FIXME: this is probably too small as default
434 stream->max_buffer_size = 230 * 1024;
436 if (stream->max_buffer_size > 1024 * 8191) {
437 av_log(ctx, AV_LOG_WARNING, "buffer size %d, too large\n", stream->max_buffer_size);
438 stream->max_buffer_size = 1024 * 8191;
442 case AVMEDIA_TYPE_SUBTITLE:
443 stream->id = mps_id++;
444 stream->max_buffer_size = 16 * 1024;
447 av_log(ctx, AV_LOG_ERROR, "Invalid media type %s for output stream #%d\n",
448 av_get_media_type_string(st->codecpar->codec_type), i);
449 return AVERROR(EINVAL);
451 stream->fifo = av_fifo_alloc(16);
458 for (i = 0; i < ctx->nb_streams; i++) {
459 AVCPBProperties *props;
461 st = ctx->streams[i];
462 stream = (StreamInfo *)st->priv_data;
464 props = (AVCPBProperties*)av_stream_get_side_data(st, AV_PKT_DATA_CPB_PROPERTIES, NULL);
466 codec_rate = props->max_bitrate;
468 codec_rate = st->codecpar->bit_rate;
471 codec_rate = (1 << 21) * 8 * 50 / ctx->nb_streams;
473 bitrate += codec_rate;
475 if ((stream->id & 0xe0) == AUDIO_ID)
476 audio_bitrate += codec_rate;
477 else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
478 video_bitrate += codec_rate;
481 if (s->user_mux_rate) {
482 s->mux_rate = (s->user_mux_rate + (8 * 50) - 1) / (8 * 50);
484 /* we increase slightly the bitrate to take into account the
485 * headers. XXX: compute it exactly */
486 bitrate += bitrate / 20;
488 s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
489 if (s->mux_rate >= (1<<22)) {
490 av_log(ctx, AV_LOG_WARNING, "mux rate %d is too large\n", s->mux_rate);
491 s->mux_rate = (1<<22) - 1;
496 int64_t overhead_rate;
498 /* The VCD standard mandates that the mux_rate field is 3528
499 * (see standard p. IV-6).
500 * The value is actually "wrong", i.e. if you calculate
501 * it using the normal formula and the 75 sectors per second transfer
502 * rate you get a different value because the real pack size is 2324,
503 * not 2352. But the standard explicitly specifies that the mux_rate
504 * field in the header must have this value. */
505 // s->mux_rate = 2352 * 75 / 50; /* = 3528 */
507 /* The VCD standard states that the muxed stream must be
508 * exactly 75 packs / second (the data rate of a single speed cdrom).
509 * Since the video bitrate (probably 1150000 bits/sec) will be below
510 * the theoretical maximum we have to add some padding packets
511 * to make up for the lower data rate.
512 * (cf. VCD standard p. IV-6 ) */
514 /* Add the header overhead to the data rate.
515 * 2279 data bytes per audio pack, 2294 data bytes per video pack */
516 overhead_rate = audio_bitrate * 2294LL * (2324 - 2279);
517 overhead_rate += video_bitrate * 2279LL * (2324 - 2294);
519 /* Add padding so that the full bitrate is 2324*75 bytes/sec */
520 s->vcd_padding_bitrate_num = (2324LL * 75 * 8 - bitrate) * 2279 * 2294 - overhead_rate;
521 #define VCD_PADDING_BITRATE_DEN (2279 * 2294)
524 if (s->is_vcd || s->is_mpeg2)
526 s->pack_header_freq = 1;
528 /* every 2 seconds */
529 s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
531 /* the above seems to make pack_header_freq zero sometimes */
532 if (s->pack_header_freq == 0)
533 s->pack_header_freq = 1;
536 /* every 200 packets. Need to look at the spec. */
537 s->system_header_freq = s->pack_header_freq * 40;
539 /* the standard mandates that there are only two system headers
540 * in the whole file: one in the first packet of each stream.
541 * (see standard p. IV-7 and IV-8) */
542 s->system_header_freq = 0x7fffffff;
544 s->system_header_freq = s->pack_header_freq * 5;
546 for (i = 0; i < ctx->nb_streams; i++) {
547 stream = ctx->streams[i]->priv_data;
548 stream->packet_number = 0;
550 s->system_header_size = get_system_header_size(ctx);
551 s->last_scr = AV_NOPTS_VALUE;
555 for (i = 0; i < ctx->nb_streams; i++)
556 av_freep(&ctx->streams[i]->priv_data);
557 return AVERROR(ENOMEM);
560 static inline void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
562 avio_w8(pb, (id << 4) | (((timestamp >> 30) & 0x07) << 1) | 1);
563 avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
564 avio_wb16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
567 /* return the number of padding bytes that should be inserted into
568 * the multiplexed stream. */
569 static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
571 MpegMuxContext *s = ctx->priv_data;
574 if (s->vcd_padding_bitrate_num > 0 && pts != AV_NOPTS_VALUE) {
575 int64_t full_pad_bytes;
577 // FIXME: this is wrong
579 av_rescale(s->vcd_padding_bitrate_num, pts, 90000LL * 8 * VCD_PADDING_BITRATE_DEN);
580 pad_bytes = (int)(full_pad_bytes - s->vcd_padding_bytes_written);
583 /* might happen if we have already padded to a later timestamp. This
584 * can occur if another stream has already advanced further. */
591 /* Write an MPEG padding packet header. */
592 static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb,
595 MpegMuxContext *s = ctx->priv_data;
598 avio_wb32(pb, PADDING_STREAM);
599 avio_wb16(pb, packet_bytes - 6);
606 for (i = 0; i < packet_bytes; i++)
610 static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len)
613 PacketDesc *pkt_desc = stream->premux_packet;
616 if (pkt_desc->size == pkt_desc->unwritten_size)
618 len -= pkt_desc->unwritten_size;
619 pkt_desc = pkt_desc->next;
625 /* flush the packet on stream stream_index */
626 static int flush_packet(AVFormatContext *ctx, int stream_index,
627 int64_t pts, int64_t dts, int64_t scr, int trailer_size)
629 MpegMuxContext *s = ctx->priv_data;
630 StreamInfo *stream = ctx->streams[stream_index]->priv_data;
632 int size, payload_size, startcode, id, stuffing_size, i, header_len;
635 int zero_trail_bytes = 0;
636 int pad_packet_bytes = 0;
638 /* "general" pack without data specific to one stream? */
639 int general_pack = 0;
644 av_log(ctx, AV_LOG_TRACE, "packet ID=%2x PTS=%0.3f\n", id, pts / 90000.0);
648 if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) {
649 /* output pack and systems header if needed */
650 size = put_pack_header(ctx, buf_ptr, scr);
655 /* there is exactly one system header for each stream in a VCD MPEG,
656 * One in the very first video packet and one in the very first
657 * audio packet (see VCD standard p. IV-7 and IV-8). */
659 if (stream->packet_number == 0) {
660 size = put_system_header(ctx, buf_ptr, id);
663 } else if (s->is_dvd) {
664 if (stream->align_iframe || s->packet_number == 0) {
665 int PES_bytes_to_fill = s->packet_size - size - 10;
667 if (pts != AV_NOPTS_VALUE) {
669 PES_bytes_to_fill -= 5 + 5;
671 PES_bytes_to_fill -= 5;
674 if (stream->bytes_to_iframe == 0 || s->packet_number == 0) {
675 size = put_system_header(ctx, buf_ptr, 0);
677 size = buf_ptr - buffer;
678 avio_write(ctx->pb, buffer, size);
680 avio_wb32(ctx->pb, PRIVATE_STREAM_2);
681 avio_wb16(ctx->pb, 0x03d4); // length
682 avio_w8(ctx->pb, 0x00); // substream ID, 00=PCI
683 for (i = 0; i < 979; i++)
684 avio_w8(ctx->pb, 0x00);
686 avio_wb32(ctx->pb, PRIVATE_STREAM_2);
687 avio_wb16(ctx->pb, 0x03fa); // length
688 avio_w8(ctx->pb, 0x01); // substream ID, 01=DSI
689 for (i = 0; i < 1017; i++)
690 avio_w8(ctx->pb, 0x00);
692 memset(buffer, 0, 128);
695 stream->align_iframe = 0;
696 // FIXME: rounding and first few bytes of each packet
697 scr += s->packet_size * 90000LL /
698 (s->mux_rate * 50LL);
699 size = put_pack_header(ctx, buf_ptr, scr);
703 } else if (stream->bytes_to_iframe < PES_bytes_to_fill) {
704 pad_packet_bytes = PES_bytes_to_fill -
705 stream->bytes_to_iframe;
709 if ((s->packet_number % s->system_header_freq) == 0) {
710 size = put_system_header(ctx, buf_ptr, 0);
715 size = buf_ptr - buffer;
716 avio_write(ctx->pb, buffer, size);
718 packet_size = s->packet_size - size;
720 if (s->is_vcd && (id & 0xe0) == AUDIO_ID)
721 /* The VCD standard demands that 20 zero bytes follow
722 * each audio pack (see standard p. IV-8). */
723 zero_trail_bytes += 20;
725 if ((s->is_vcd && stream->packet_number == 0) ||
726 (s->is_svcd && s->packet_number == 0)) {
727 /* for VCD the first pack of each stream contains only the pack header,
728 * the system header and lots of padding (see VCD standard p. IV-6).
729 * In the case of an audio pack, 20 zero bytes are also added at
731 /* For SVCD we fill the very first pack to increase compatibility with
732 * some DVD players. Not mandated by the standard. */
734 /* the system header refers to both streams and no stream data */
736 pad_packet_bytes = packet_size - zero_trail_bytes;
739 packet_size -= pad_packet_bytes + zero_trail_bytes;
741 if (packet_size > 0) {
742 /* packet header size */
748 if (stream->packet_number == 0)
749 header_len += 3; /* PES extension */
750 header_len += 1; /* obligatory stuffing byte */
754 if (pts != AV_NOPTS_VALUE) {
764 payload_size = packet_size - header_len;
766 startcode = PRIVATE_STREAM_1;
774 startcode = 0x100 + id;
777 stuffing_size = payload_size - av_fifo_size(stream->fifo);
779 // first byte does not fit -> reset pts/dts + stuffing
780 if (payload_size <= trailer_size && pts != AV_NOPTS_VALUE) {
781 int timestamp_len = 0;
784 if (pts != AV_NOPTS_VALUE)
785 timestamp_len += s->is_mpeg2 ? 5 : 4;
787 dts = AV_NOPTS_VALUE;
788 header_len -= timestamp_len;
789 if (s->is_dvd && stream->align_iframe) {
790 pad_packet_bytes += timestamp_len;
791 packet_size -= timestamp_len;
793 payload_size += timestamp_len;
795 stuffing_size += timestamp_len;
796 if (payload_size > trailer_size)
797 stuffing_size += payload_size - trailer_size;
800 // can't use padding, so use stuffing
801 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) {
802 packet_size += pad_packet_bytes;
803 payload_size += pad_packet_bytes; // undo the previous adjustment
804 if (stuffing_size < 0)
805 stuffing_size = pad_packet_bytes;
807 stuffing_size += pad_packet_bytes;
808 pad_packet_bytes = 0;
811 if (stuffing_size < 0)
814 if (startcode == PRIVATE_STREAM_1 && id >= 0xa0) {
815 if (payload_size < av_fifo_size(stream->fifo))
816 stuffing_size += payload_size % stream->lpcm_align;
819 if (stuffing_size > 16) { /* <=16 for MPEG-1, <=32 for MPEG-2 */
820 pad_packet_bytes += stuffing_size;
821 packet_size -= stuffing_size;
822 payload_size -= stuffing_size;
826 nb_frames = get_nb_frames(ctx, stream, payload_size - stuffing_size);
828 avio_wb32(ctx->pb, startcode);
830 avio_wb16(ctx->pb, packet_size);
833 for (i = 0; i < stuffing_size; i++)
834 avio_w8(ctx->pb, 0xff);
837 avio_w8(ctx->pb, 0x80); /* mpeg2 id */
841 if (pts != AV_NOPTS_VALUE) {
847 /* Both the MPEG-2 and the SVCD standards demand that the
848 * P-STD_buffer_size field be included in the first packet of
849 * every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
850 * and MPEG-2 standard 2.7.7) */
851 if (stream->packet_number == 0)
854 avio_w8(ctx->pb, pes_flags); /* flags */
855 avio_w8(ctx->pb, header_len - 3 + stuffing_size);
857 if (pes_flags & 0x80) /* write pts */
858 put_timestamp(ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
859 if (pes_flags & 0x40) /* write dts */
860 put_timestamp(ctx->pb, 0x01, dts);
862 if (pes_flags & 0x01) { /* write pes extension */
863 avio_w8(ctx->pb, 0x10); /* flags */
865 /* P-STD buffer info */
866 if ((id & 0xe0) == AUDIO_ID)
867 avio_wb16(ctx->pb, 0x4000 | stream->max_buffer_size / 128);
869 avio_wb16(ctx->pb, 0x6000 | stream->max_buffer_size / 1024);
872 if (pts != AV_NOPTS_VALUE) {
874 put_timestamp(ctx->pb, 0x03, pts);
875 put_timestamp(ctx->pb, 0x01, dts);
877 put_timestamp(ctx->pb, 0x02, pts);
880 avio_w8(ctx->pb, 0x0f);
885 /* special stuffing byte that is always written
886 * to prevent accidental generation of start codes. */
887 avio_w8(ctx->pb, 0xff);
889 for (i = 0; i < stuffing_size; i++)
890 avio_w8(ctx->pb, 0xff);
893 if (startcode == PRIVATE_STREAM_1) {
894 avio_w8(ctx->pb, id);
896 /* LPCM (XXX: check nb_frames) */
898 avio_wb16(ctx->pb, 4); /* skip 3 header bytes */
899 avio_w8(ctx->pb, stream->lpcm_header[0]);
900 avio_w8(ctx->pb, stream->lpcm_header[1]);
901 avio_w8(ctx->pb, stream->lpcm_header[2]);
902 } else if (id >= 0x40) {
904 avio_w8(ctx->pb, nb_frames);
905 avio_wb16(ctx->pb, trailer_size + 1);
910 av_assert0(payload_size - stuffing_size <= av_fifo_size(stream->fifo));
911 av_fifo_generic_read(stream->fifo, ctx->pb,
912 payload_size - stuffing_size,
913 (void (*)(void*, void*, int))avio_write);
914 stream->bytes_to_iframe -= payload_size - stuffing_size;
920 if (pad_packet_bytes > 0)
921 put_padding_packet(ctx, ctx->pb, pad_packet_bytes);
923 for (i = 0; i < zero_trail_bytes; i++)
924 avio_w8(ctx->pb, 0x00);
930 /* only increase the stream packet number if this pack actually contains
931 * something that is specific to this stream! I.e. a dedicated header
934 stream->packet_number++;
936 return payload_size - stuffing_size;
939 static void put_vcd_padding_sector(AVFormatContext *ctx)
941 /* There are two ways to do this padding: writing a sector/pack
942 * of 0 values, or writing an MPEG padding pack. Both seem to
943 * work with most decoders, BUT the VCD standard only allows a 0-sector
944 * (see standard p. IV-4, IV-5).
945 * So a 0-sector it is... */
947 MpegMuxContext *s = ctx->priv_data;
950 for (i = 0; i < s->packet_size; i++)
953 s->vcd_padding_bytes_written += s->packet_size;
957 /* increasing the packet number is correct. The SCR of the following packs
958 * is calculated from the packet_number and it has to include the padding
959 * sector (it represents the sector index, not the MPEG pack index)
960 * (see VCD standard p. IV-6) */
964 static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr)
968 for (i = 0; i < ctx->nb_streams; i++) {
969 AVStream *st = ctx->streams[i];
970 StreamInfo *stream = st->priv_data;
971 PacketDesc *pkt_desc;
973 while ((pkt_desc = stream->predecode_packet) &&
974 scr > pkt_desc->dts) { // FIXME: > vs >=
975 if (stream->buffer_index < pkt_desc->size ||
976 stream->predecode_packet == stream->premux_packet) {
977 av_log(ctx, AV_LOG_ERROR,
978 "buffer underflow st=%d bufi=%d size=%d\n",
979 i, stream->buffer_index, pkt_desc->size);
982 stream->buffer_index -= pkt_desc->size;
983 stream->predecode_packet = pkt_desc->next;
991 static int output_packet(AVFormatContext *ctx, int flush)
993 MpegMuxContext *s = ctx->priv_data;
996 int i, avail_space = 0, es_size, trailer_size;
998 int best_score = INT_MIN;
999 int ignore_constraints = 0;
1000 int ignore_delay = 0;
1001 int64_t scr = s->last_scr;
1002 PacketDesc *timestamp_packet;
1003 const int64_t max_delay = av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
1006 for (i = 0; i < ctx->nb_streams; i++) {
1007 AVStream *st = ctx->streams[i];
1008 StreamInfo *stream = st->priv_data;
1009 const int avail_data = av_fifo_size(stream->fifo);
1010 const int space = stream->max_buffer_size - stream->buffer_index;
1011 int rel_space = 1024LL * space / stream->max_buffer_size;
1012 PacketDesc *next_pkt = stream->premux_packet;
1014 /* for subtitle, a single PES packet must be generated,
1015 * so we flush after every single subtitle packet */
1016 if (s->packet_size > avail_data && !flush
1017 && st->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE)
1019 if (avail_data == 0)
1021 av_assert0(avail_data > 0);
1023 if (space < s->packet_size && !ignore_constraints)
1026 if (next_pkt && next_pkt->dts - scr > max_delay && !ignore_delay)
1028 if ( stream->predecode_packet
1029 && stream->predecode_packet->size > stream->buffer_index)
1031 if (rel_space > best_score) {
1032 best_score = rel_space;
1034 avail_space = space;
1039 int64_t best_dts = INT64_MAX;
1042 for (i = 0; i < ctx->nb_streams; i++) {
1043 AVStream *st = ctx->streams[i];
1044 StreamInfo *stream = st->priv_data;
1045 PacketDesc *pkt_desc = stream->predecode_packet;
1046 if (pkt_desc && pkt_desc->dts < best_dts)
1047 best_dts = pkt_desc->dts;
1048 has_premux |= !!stream->premux_packet;
1051 if (best_dts < INT64_MAX) {
1052 av_log(ctx, AV_LOG_TRACE, "bumping scr, scr:%f, dts:%f\n",
1053 scr / 90000.0, best_dts / 90000.0);
1055 if (scr >= best_dts + 1 && !ignore_constraints) {
1056 av_log(ctx, AV_LOG_ERROR,
1057 "packet too large, ignoring buffer limits to mux it\n");
1058 ignore_constraints = 1;
1060 scr = FFMAX(best_dts + 1, scr);
1061 if (remove_decoded_packets(ctx, scr) < 0)
1063 } else if (has_premux && flush) {
1064 av_log(ctx, AV_LOG_ERROR,
1065 "delay too large, ignoring ...\n");
1067 ignore_constraints = 1;
1074 av_assert0(best_i >= 0);
1076 st = ctx->streams[best_i];
1077 stream = st->priv_data;
1079 av_assert0(av_fifo_size(stream->fifo) > 0);
1081 av_assert0(avail_space >= s->packet_size || ignore_constraints);
1083 timestamp_packet = stream->premux_packet;
1084 if (timestamp_packet->unwritten_size == timestamp_packet->size) {
1087 trailer_size = timestamp_packet->unwritten_size;
1088 timestamp_packet = timestamp_packet->next;
1091 if (timestamp_packet) {
1092 av_log(ctx, AV_LOG_TRACE, "dts:%f pts:%f scr:%f stream:%d\n",
1093 timestamp_packet->dts / 90000.0,
1094 timestamp_packet->pts / 90000.0,
1095 scr / 90000.0, best_i);
1096 es_size = flush_packet(ctx, best_i, timestamp_packet->pts,
1097 timestamp_packet->dts, scr, trailer_size);
1099 av_assert0(av_fifo_size(stream->fifo) == trailer_size);
1100 es_size = flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr,
1105 /* Write one or more padding sectors, if necessary, to reach
1106 * the constant overall bitrate. */
1109 // FIXME: pts cannot be correct here
1110 while ((vcd_pad_bytes = get_vcd_padding_size(ctx, stream->premux_packet->pts)) >= s->packet_size) {
1111 put_vcd_padding_sector(ctx);
1112 // FIXME: rounding and first few bytes of each packet
1113 s->last_scr += s->packet_size * 90000LL / (s->mux_rate * 50LL);
1117 stream->buffer_index += es_size;
1118 // FIXME: rounding and first few bytes of each packet
1119 s->last_scr += s->packet_size * 90000LL / (s->mux_rate * 50LL);
1121 while (stream->premux_packet &&
1122 stream->premux_packet->unwritten_size <= es_size) {
1123 es_size -= stream->premux_packet->unwritten_size;
1124 stream->premux_packet = stream->premux_packet->next;
1127 av_assert0(stream->premux_packet);
1128 stream->premux_packet->unwritten_size -= es_size;
1131 if (remove_decoded_packets(ctx, s->last_scr) < 0)
1137 static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
1139 int stream_index = pkt->stream_index;
1140 int size = pkt->size;
1141 uint8_t *buf = pkt->data;
1142 MpegMuxContext *s = ctx->priv_data;
1143 AVStream *st = ctx->streams[stream_index];
1144 StreamInfo *stream = st->priv_data;
1146 PacketDesc *pkt_desc;
1148 const int is_iframe = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1149 (pkt->flags & AV_PKT_FLAG_KEY);
1151 preload = av_rescale(s->preload, 90000, AV_TIME_BASE);
1156 if (s->last_scr == AV_NOPTS_VALUE) {
1157 if (dts == AV_NOPTS_VALUE || (dts < preload && ctx->avoid_negative_ts) || s->is_dvd) {
1158 if (dts != AV_NOPTS_VALUE)
1159 s->preload += av_rescale(-dts, AV_TIME_BASE, 90000);
1162 s->last_scr = dts - preload;
1165 preload = av_rescale(s->preload, 90000, AV_TIME_BASE);
1166 av_log(ctx, AV_LOG_DEBUG, "First SCR: %"PRId64" First DTS: %"PRId64"\n", s->last_scr, dts + preload);
1169 if (dts != AV_NOPTS_VALUE) dts += preload;
1170 if (pts != AV_NOPTS_VALUE) pts += preload;
1172 av_log(ctx, AV_LOG_TRACE, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n",
1173 dts / 90000.0, pts / 90000.0, pkt->flags,
1174 pkt->stream_index, pts != AV_NOPTS_VALUE);
1175 if (!stream->premux_packet)
1176 stream->next_packet = &stream->premux_packet;
1177 *stream->next_packet =
1178 pkt_desc = av_mallocz(sizeof(PacketDesc));
1180 return AVERROR(ENOMEM);
1181 pkt_desc->pts = pts;
1182 pkt_desc->dts = dts;
1184 if (st->codecpar->codec_id == AV_CODEC_ID_PCM_DVD) {
1186 av_log(ctx, AV_LOG_ERROR, "Invalid packet size %d\n", size);
1187 return AVERROR(EINVAL);
1190 /* Skip first 3 bytes of packet data, which comprise PCM header
1191 and will be written fresh by this muxer. */
1196 pkt_desc->unwritten_size =
1197 pkt_desc->size = size;
1198 if (!stream->predecode_packet)
1199 stream->predecode_packet = pkt_desc;
1200 stream->next_packet = &pkt_desc->next;
1202 if (av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size) < 0)
1206 // min VOBU length 0.4 seconds (mpucoder)
1208 (s->packet_number == 0 ||
1209 (pts - stream->vobu_start_pts >= 36000))) {
1210 stream->bytes_to_iframe = av_fifo_size(stream->fifo);
1211 stream->align_iframe = 1;
1212 stream->vobu_start_pts = pts;
1216 av_fifo_generic_write(stream->fifo, buf, size, NULL);
1219 int ret = output_packet(ctx, 0);
1225 static int mpeg_mux_end(AVFormatContext *ctx)
1231 int ret = output_packet(ctx, 1);
1238 /* End header according to MPEG-1 systems standard. We do not write
1239 * it as it is usually not needed by decoders and because it
1240 * complicates MPEG stream concatenation. */
1241 // avio_wb32(ctx->pb, ISO_11172_END_CODE);
1242 // avio_flush(ctx->pb);
1244 for (i = 0; i < ctx->nb_streams; i++) {
1245 stream = ctx->streams[i]->priv_data;
1247 av_assert0(av_fifo_size(stream->fifo) == 0);
1248 av_fifo_freep(&stream->fifo);
1253 #define OFFSET(x) offsetof(MpegMuxContext, x)
1254 #define E AV_OPT_FLAG_ENCODING_PARAM
1255 static const AVOption options[] = {
1256 { "muxrate", NULL, OFFSET(user_mux_rate), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, ((1<<22) - 1) * (8 * 50), E },
1257 { "preload", "Initial demux-decode delay in microseconds.", OFFSET(preload), AV_OPT_TYPE_INT, { .i64 = 500000 }, 0, INT_MAX, E },
1261 #define MPEGENC_CLASS(flavor) \
1262 static const AVClass flavor ## _class = { \
1263 .class_name = #flavor " muxer", \
1264 .item_name = av_default_item_name, \
1265 .version = LIBAVUTIL_VERSION_INT, \
1266 .option = options, \
1269 #if CONFIG_MPEG1SYSTEM_MUXER
1271 AVOutputFormat ff_mpeg1system_muxer = {
1273 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream"),
1274 .mime_type = "video/mpeg",
1275 .extensions = "mpg,mpeg",
1276 .priv_data_size = sizeof(MpegMuxContext),
1277 .audio_codec = AV_CODEC_ID_MP2,
1278 .video_codec = AV_CODEC_ID_MPEG1VIDEO,
1279 .write_header = mpeg_mux_init,
1280 .write_packet = mpeg_mux_write_packet,
1281 .write_trailer = mpeg_mux_end,
1282 .priv_class = &mpeg_class,
1286 #if CONFIG_MPEG1VCD_MUXER
1288 AVOutputFormat ff_mpeg1vcd_muxer = {
1290 .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream (VCD)"),
1291 .mime_type = "video/mpeg",
1292 .priv_data_size = sizeof(MpegMuxContext),
1293 .audio_codec = AV_CODEC_ID_MP2,
1294 .video_codec = AV_CODEC_ID_MPEG1VIDEO,
1295 .write_header = mpeg_mux_init,
1296 .write_packet = mpeg_mux_write_packet,
1297 .write_trailer = mpeg_mux_end,
1298 .priv_class = &vcd_class,
1302 #if CONFIG_MPEG2VOB_MUXER
1304 AVOutputFormat ff_mpeg2vob_muxer = {
1306 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (VOB)"),
1307 .mime_type = "video/mpeg",
1308 .extensions = "vob",
1309 .priv_data_size = sizeof(MpegMuxContext),
1310 .audio_codec = AV_CODEC_ID_MP2,
1311 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1312 .write_header = mpeg_mux_init,
1313 .write_packet = mpeg_mux_write_packet,
1314 .write_trailer = mpeg_mux_end,
1315 .priv_class = &vob_class,
1319 /* Same as mpeg2vob_mux except that the pack size is 2324 */
1320 #if CONFIG_MPEG2SVCD_MUXER
1322 AVOutputFormat ff_mpeg2svcd_muxer = {
1324 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (SVCD)"),
1325 .mime_type = "video/mpeg",
1326 .extensions = "vob",
1327 .priv_data_size = sizeof(MpegMuxContext),
1328 .audio_codec = AV_CODEC_ID_MP2,
1329 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1330 .write_header = mpeg_mux_init,
1331 .write_packet = mpeg_mux_write_packet,
1332 .write_trailer = mpeg_mux_end,
1333 .priv_class = &svcd_class,
1337 /* Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
1338 #if CONFIG_MPEG2DVD_MUXER
1340 AVOutputFormat ff_mpeg2dvd_muxer = {
1342 .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (DVD VOB)"),
1343 .mime_type = "video/mpeg",
1344 .extensions = "dvd",
1345 .priv_data_size = sizeof(MpegMuxContext),
1346 .audio_codec = AV_CODEC_ID_MP2,
1347 .video_codec = AV_CODEC_ID_MPEG2VIDEO,
1348 .write_header = mpeg_mux_init,
1349 .write_packet = mpeg_mux_write_packet,
1350 .write_trailer = mpeg_mux_end,
1351 .priv_class = &dvd_class,