36 #define MAX_PAYLOAD_SIZE 4096
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));
130 int only_for_stream_id)
133 int size, i, private_stream_coded,
id;
192 int P_STD_max_video = 0;
193 int P_STD_max_mpeg_audio = 0;
194 int P_STD_max_mpeg_PS1 = 0;
202 }
else if (
id >= 0xc0 &&
id <= 0xc7 &&
205 }
else if (
id == 0xe0 &&
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;
223 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
229 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
238 private_stream_coded = 0;
245 if (!s->
is_vcd || stream->
id == only_for_stream_id ||
246 only_for_stream_id == 0) {
250 if (private_stream_coded)
252 private_stream_coded = 1;
280 int buf_index, i, private_stream_coded;
288 private_stream_coded = 0;
291 if (stream->
id < 0xc0) {
292 if (private_stream_coded)
294 private_stream_coded = 1;
304 int bitrate, i, mpa_id, mpv_id, h264_id, mps_id, ac3_id, dts_id, lpcm_id, j;
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);
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",
365 stream->
id = ac3_id++;
367 stream->
id = dts_id++;
369 stream->
id = lpcm_id++;
370 for (j = 0; j < 4; j++) {
383 stream->
id = mpa_id++;
393 stream->
id = h264_id++;
395 stream->
id = mpv_id++;
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");
413 stream->
id = mps_id++;
438 codec_rate = (1 << 21) * 8 * 50 / ctx->
nb_streams;
440 bitrate += codec_rate;
443 audio_bitrate += codec_rate;
445 video_bitrate += codec_rate;
453 bitrate += bitrate / 20;
455 s->
mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
463 double overhead_rate;
483 overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
484 overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
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));
542 int64_t full_pad_bytes;
573 for (i = 0; i < packet_bytes; i++)
586 pkt_desc = pkt_desc->
next;
594 int64_t pts, int64_t dts, int64_t scr,
int trailer_size)
599 int size, payload_size, startcode,
id, stuffing_size, i, header_len;
602 int zero_trail_bytes = 0;
603 int pad_packet_bytes = 0;
606 int general_pack = 0;
611 av_dlog(ctx,
"packet ID=%2x PTS=%0.3f\n",
id, pts / 90000.0);
632 int PES_bytes_to_fill = s->
packet_size - size - 10;
636 PES_bytes_to_fill -= 5 + 5;
638 PES_bytes_to_fill -= 5;
650 for (i = 0; i < 979; i++)
656 for (i = 0; i < 1017; i++)
659 memset(buffer, 0, 128);
671 pad_packet_bytes = PES_bytes_to_fill -
690 zero_trail_bytes += 20;
703 pad_packet_bytes = packet_size - zero_trail_bytes;
706 packet_size -= pad_packet_bytes + zero_trail_bytes;
708 if (packet_size > 0) {
731 payload_size = packet_size - header_len;
741 startcode = 0x100 +
id;
748 int timestamp_len = 0;
752 timestamp_len += s->
is_mpeg2 ? 5 : 4;
755 header_len -= timestamp_len;
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;
768 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) {
769 packet_size += pad_packet_bytes;
770 payload_size += pad_packet_bytes;
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)
783 stuffing_size += payload_size % stream->
lpcm_align;
786 if (stuffing_size > 16) {
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);
800 for (i = 0; i < stuffing_size; i++)
822 avio_w8(ctx->
pb, header_len - 3 + stuffing_size);
824 if (pes_flags & 0x80)
826 if (pes_flags & 0x40)
829 if (pes_flags & 0x01) {
856 for (i = 0; i < stuffing_size; i++)
869 }
else if (
id >= 0x40) {
879 payload_size - stuffing_size,
887 if (pad_packet_bytes > 0)
890 for (i = 0; i < zero_trail_bytes; i++)
903 return payload_size - stuffing_size;
941 scr > pkt_desc->
dts) {
945 "buffer underflow st=%d bufi=%d size=%d\n",
963 int i, avail_space = 0, es_size, trailer_size;
965 int best_score = INT_MIN;
966 int ignore_constraints = 0;
989 if (space < s->packet_size && !ignore_constraints)
992 if (next_pkt && next_pkt->
dts - scr > max_delay)
997 if (rel_space > best_score) {
998 best_score = rel_space;
1000 avail_space = space;
1005 int64_t best_dts = INT64_MAX;
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) {
1022 "packet too large, ignoring buffer limits to mux it\n");
1023 ignore_constraints = 1;
1025 scr =
FFMAX(best_dts + 1, scr);
1031 assert(best_i >= 0);
1038 assert(avail_space >= s->
packet_size || ignore_constraints);
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);
1054 timestamp_packet->
dts, scr, trailer_size);
1129 av_dlog(ctx,
"dts:%f pts:%f flags:%d stream:%d nopts:%d\n",
1130 dts / 90000.0, pts / 90000.0, pkt->
flags,
1136 pkt_desc->
pts = pts;
1137 pkt_desc->
dts = dts;
1195 #define OFFSET(x) offsetof(MpegMuxContext, x)
1196 #define E AV_OPT_FLAG_ENCODING_PARAM
1198 {
"muxrate", NULL,
OFFSET(user_mux_rate),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, ((1<<22) - 1) * (8 * 50),
E },
1199 {
"preload",
"Initial demux-decode delay in microseconds.",
OFFSET(preload),
AV_OPT_TYPE_INT, { .i64 = 500000 }, 0, INT_MAX,
E },
1203 #define MPEGENC_CLASS(flavor) \
1204 static const AVClass flavor ## _class = { \
1205 .class_name = #flavor " muxer", \
1206 .item_name = av_default_item_name, \
1207 .version = LIBAVUTIL_VERSION_INT, \
1208 .option = options, \
1211 #if CONFIG_MPEG1SYSTEM_MUXER
1216 .mime_type =
"video/mpeg",
1217 .extensions =
"mpg,mpeg",
1224 .priv_class = &mpeg_class,
1228 #if CONFIG_MPEG1VCD_MUXER
1233 .mime_type =
"video/mpeg",
1240 .priv_class = &vcd_class,
1244 #if CONFIG_MPEG2VOB_MUXER
1249 .mime_type =
"video/mpeg",
1250 .extensions =
"vob",
1257 .priv_class = &vob_class,
1262 #if CONFIG_MPEG2SVCD_MUXER
1267 .mime_type =
"video/mpeg",
1268 .extensions =
"vob",
1275 .priv_class = &svcd_class,
1280 #if CONFIG_MPEG2DVD_MUXER
1285 .mime_type =
"video/mpeg",
1286 .extensions =
"dvd",
1293 .priv_class = &dvd_class,