36 #define MAX_PAYLOAD_SIZE 4096
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));
126 int only_for_stream_id)
129 int size, i, private_stream_coded,
id;
188 int P_STD_max_video = 0;
189 int P_STD_max_mpeg_audio = 0;
190 int P_STD_max_mpeg_PS1 = 0;
198 }
else if (
id >= 0xc0 &&
id <= 0xc7 &&
201 }
else if (
id == 0xe0 &&
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;
219 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
225 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
234 private_stream_coded = 0;
241 if (!s->
is_vcd || stream->
id == only_for_stream_id ||
242 only_for_stream_id == 0) {
246 if (private_stream_coded)
248 private_stream_coded = 1;
276 int buf_index, i, private_stream_coded;
284 private_stream_coded = 0;
287 if (stream->
id < 0xc0) {
288 if (private_stream_coded)
290 private_stream_coded = 1;
300 int bitrate, i, mpa_id, mpv_id, h264_id, mps_id, ac3_id, dts_id, lpcm_id, j;
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);
356 "%s in MPEG-1 system streams is not widely supported, "
357 "consider using the vob or the dvd muxer "
358 "to force a MPEG-2 program stream.\n",
361 stream->
id = ac3_id++;
363 stream->
id = dts_id++;
365 stream->
id = lpcm_id++;
366 for (j = 0; j < 4; j++) {
379 stream->
id = mpa_id++;
389 stream->
id = h264_id++;
391 stream->
id = mpv_id++;
396 "VBV buffer size not set, using default size of 130KB\n"
397 "If you want the mpeg file to be compliant to some specification\n"
398 "Like DVD, VCD or others, make sure you set the correct buffer size\n");
409 stream->
id = mps_id++;
434 codec_rate = (1 << 21) * 8 * 50 / ctx->
nb_streams;
436 bitrate += codec_rate;
439 audio_bitrate += codec_rate;
441 video_bitrate += codec_rate;
449 bitrate += bitrate / 20;
451 s->
mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
459 double overhead_rate;
479 overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
480 overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
525 avio_w8(pb, (
id << 4) | (((timestamp >> 30) & 0x07) << 1) | 1);
526 avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
527 avio_wb16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
538 int64_t full_pad_bytes;
569 for (i = 0; i < packet_bytes; i++)
582 pkt_desc = pkt_desc->
next;
590 int64_t
pts, int64_t dts, int64_t scr,
int trailer_size)
595 int size, payload_size, startcode,
id, stuffing_size, i, header_len;
598 int zero_trail_bytes = 0;
599 int pad_packet_bytes = 0;
602 int general_pack = 0;
607 av_dlog(ctx,
"packet ID=%2x PTS=%0.3f\n",
id, pts / 90000.0);
628 int PES_bytes_to_fill = s->
packet_size - size - 10;
632 PES_bytes_to_fill -= 5 + 5;
634 PES_bytes_to_fill -= 5;
646 for (i = 0; i < 979; i++)
652 for (i = 0; i < 1017; i++)
655 memset(buffer, 0, 128);
667 pad_packet_bytes = PES_bytes_to_fill -
686 zero_trail_bytes += 20;
699 pad_packet_bytes = packet_size - zero_trail_bytes;
702 packet_size -= pad_packet_bytes + zero_trail_bytes;
704 if (packet_size > 0) {
727 payload_size = packet_size - header_len;
737 startcode = 0x100 +
id;
744 int timestamp_len = 0;
748 timestamp_len += s->
is_mpeg2 ? 5 : 4;
751 header_len -= timestamp_len;
753 pad_packet_bytes += timestamp_len;
754 packet_size -= timestamp_len;
756 payload_size += timestamp_len;
758 stuffing_size += timestamp_len;
759 if (payload_size > trailer_size)
760 stuffing_size += payload_size - trailer_size;
764 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) {
765 packet_size += pad_packet_bytes;
766 payload_size += pad_packet_bytes;
767 if (stuffing_size < 0)
768 stuffing_size = pad_packet_bytes;
770 stuffing_size += pad_packet_bytes;
771 pad_packet_bytes = 0;
774 if (stuffing_size < 0)
779 stuffing_size += payload_size % stream->
lpcm_align;
782 if (stuffing_size > 16) {
783 pad_packet_bytes += stuffing_size;
784 packet_size -= stuffing_size;
785 payload_size -= stuffing_size;
789 nb_frames =
get_nb_frames(ctx, stream, payload_size - stuffing_size);
796 for (i = 0; i < stuffing_size; i++)
818 avio_w8(ctx->
pb, header_len - 3 + stuffing_size);
820 if (pes_flags & 0x80)
822 if (pes_flags & 0x40)
825 if (pes_flags & 0x01) {
852 for (i = 0; i < stuffing_size; i++)
865 }
else if (
id >= 0x40) {
875 payload_size - stuffing_size,
883 if (pad_packet_bytes > 0)
886 for (i = 0; i < zero_trail_bytes; i++)
899 return payload_size - stuffing_size;
937 scr > pkt_desc->
dts) {
941 "buffer underflow st=%d bufi=%d size=%d\n",
959 int i, avail_space = 0, es_size, trailer_size;
961 int best_score = INT_MIN;
962 int ignore_constraints = 0;
963 int ignore_delay = 0;
986 if (space < s->packet_size && !ignore_constraints)
989 if (next_pkt && next_pkt->
dts - scr > max_delay && !ignore_delay)
994 if (rel_space > best_score) {
995 best_score = rel_space;
1002 int64_t best_dts = INT64_MAX;
1009 if (pkt_desc && pkt_desc->
dts < best_dts)
1010 best_dts = pkt_desc->
dts;
1014 if (best_dts < INT64_MAX) {
1015 av_dlog(ctx,
"bumping scr, scr:%f, dts:%f\n",
1016 scr / 90000.0, best_dts / 90000.0);
1018 if (scr >= best_dts + 1 && !ignore_constraints) {
1020 "packet too large, ignoring buffer limits to mux it\n");
1021 ignore_constraints = 1;
1023 scr =
FFMAX(best_dts + 1, scr);
1026 }
else if (has_premux && flush) {
1028 "delay too large, ignoring ...\n");
1030 ignore_constraints = 1;
1051 timestamp_packet = timestamp_packet->
next;
1054 if (timestamp_packet) {
1055 av_dlog(ctx,
"dts:%f pts:%f scr:%f stream:%d\n",
1056 timestamp_packet->
dts / 90000.0,
1057 timestamp_packet->
pts / 90000.0,
1058 scr / 90000.0, best_i);
1060 timestamp_packet->
dts, scr, trailer_size);
1135 av_dlog(ctx,
"dts:%f pts:%f flags:%d stream:%d nopts:%d\n",
1136 dts / 90000.0, pts / 90000.0, pkt->
flags,
1143 pkt_desc->
dts = dts;
1201 #define OFFSET(x) offsetof(MpegMuxContext, x)
1202 #define E AV_OPT_FLAG_ENCODING_PARAM
1205 {
"preload",
"Initial demux-decode delay in microseconds.",
OFFSET(preload),
AV_OPT_TYPE_INT, { .i64 = 500000 }, 0, INT_MAX,
E },
1209 #define MPEGENC_CLASS(flavor) \
1210 static const AVClass flavor ## _class = { \
1211 .class_name = #flavor " muxer", \
1212 .item_name = av_default_item_name, \
1213 .version = LIBAVUTIL_VERSION_INT, \
1214 .option = options, \
1217 #if CONFIG_MPEG1SYSTEM_MUXER
1222 .mime_type =
"video/mpeg",
1223 .extensions =
"mpg,mpeg",
1230 .priv_class = &mpeg_class,
1234 #if CONFIG_MPEG1VCD_MUXER
1239 .mime_type =
"video/mpeg",
1246 .priv_class = &vcd_class,
1250 #if CONFIG_MPEG2VOB_MUXER
1255 .mime_type =
"video/mpeg",
1256 .extensions =
"vob",
1263 .priv_class = &vob_class,
1268 #if CONFIG_MPEG2SVCD_MUXER
1273 .mime_type =
"video/mpeg",
1274 .extensions =
"vob",
1281 .priv_class = &svcd_class,
1286 #if CONFIG_MPEG2DVD_MUXER
1291 .mime_type =
"video/mpeg",
1292 .extensions =
"dvd",
1299 .priv_class = &dvd_class,