31 #define MAX_PAYLOAD_SIZE 4096
91 uint8_t *buf, int64_t timestamp)
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));
129 int size, i, private_stream_coded,
id;
185 int P_STD_max_video = 0;
186 int P_STD_max_mpeg_audio = 0;
187 int P_STD_max_mpeg_PS1 = 0;
195 }
else if (
id >= 0xc0 && id <= 0xc7 && stream->max_buffer_size > P_STD_max_mpeg_audio) {
206 put_bits(&pb, 13, P_STD_max_video / 1024);
209 if (P_STD_max_mpeg_audio == 0)
210 P_STD_max_mpeg_audio = 4096;
214 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
220 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
230 private_stream_coded = 0;
238 if ( !s->
is_vcd || stream->
id==only_for_stream_id
239 || only_for_stream_id==0) {
244 if (private_stream_coded)
246 private_stream_coded = 1;
267 buf[4] = (size - 6) >> 8;
268 buf[5] = (size - 6) & 0xff;
275 int buf_index, i, private_stream_coded;
283 private_stream_coded = 0;
286 if (stream->
id < 0xc0) {
287 if (private_stream_coded)
289 private_stream_coded = 1;
299 int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j;
306 s->
is_vcd = (CONFIG_MPEG1VCD_MUXER && ctx->
oformat == &ff_mpeg1vcd_muxer);
307 s->
is_svcd = (CONFIG_MPEG2SVCD_MUXER && ctx->
oformat == &ff_mpeg2svcd_muxer);
308 s->
is_mpeg2 = ((CONFIG_MPEG2VOB_MUXER && ctx->
oformat == &ff_mpeg2vob_muxer) ||
309 (CONFIG_MPEG2DVD_MUXER && ctx->
oformat == &ff_mpeg2dvd_muxer) ||
310 (CONFIG_MPEG2SVCD_MUXER && ctx->
oformat == &ff_mpeg2svcd_muxer));
311 s->
is_dvd = (CONFIG_MPEG2DVD_MUXER && ctx->
oformat == &ff_mpeg2dvd_muxer);
348 stream->
id = ac3_id++;
350 stream->
id = dts_id++;
352 stream->
id = lpcm_id++;
353 for(j = 0; j < 4; j++) {
366 stream->
id = mpa_id++;
375 stream->
id = mpv_id++;
385 stream->
id = mps_id++;
411 bitrate += codec_rate;
414 audio_bitrate += codec_rate;
416 video_bitrate += codec_rate;
424 bitrate += bitrate / 20;
426 s->
mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
430 double overhead_rate;
450 overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
451 overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
498 (((timestamp >> 30) & 0x07) << 1) |
500 avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
501 avio_wb16(pb, (uint16_t)((((timestamp ) & 0x7fff) << 1) | 1));
514 int64_t full_pad_bytes;
543 for(i=0;i<packet_bytes;i++)
555 pkt_desc= pkt_desc->
next;
563 int64_t pts, int64_t dts, int64_t scr,
int trailer_size)
568 int size, payload_size, startcode,
id, stuffing_size, i, header_len;
571 int zero_trail_bytes = 0;
572 int pad_packet_bytes = 0;
574 int general_pack = 0;
579 av_dlog(ctx,
"packet ID=%2x PTS=%0.3f\n",
id, pts / 90000.0);
600 int PES_bytes_to_fill = s->
packet_size - size - 10;
604 PES_bytes_to_fill -= 5 + 5;
606 PES_bytes_to_fill -= 5;
618 for (i = 0; i < 979; i++)
624 for (i = 0; i < 1017; i++)
627 memset(buffer, 0, 128);
655 zero_trail_bytes += 20;
667 pad_packet_bytes = packet_size - zero_trail_bytes;
670 packet_size -= pad_packet_bytes + zero_trail_bytes;
672 if (packet_size > 0) {
696 payload_size = packet_size - header_len;
706 startcode = 0x100 +
id;
717 timestamp_len += s->
is_mpeg2 ? 5 : 4;
719 header_len -= timestamp_len;
721 pad_packet_bytes += timestamp_len;
722 packet_size -= timestamp_len;
724 payload_size += timestamp_len;
726 stuffing_size += timestamp_len;
727 if(payload_size > trailer_size)
728 stuffing_size += payload_size - trailer_size;
731 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) {
732 packet_size += pad_packet_bytes;
733 payload_size += pad_packet_bytes;
734 if (stuffing_size < 0) {
735 stuffing_size = pad_packet_bytes;
737 stuffing_size += pad_packet_bytes;
739 pad_packet_bytes = 0;
742 if (stuffing_size < 0)
747 stuffing_size += payload_size % stream->
lpcm_align;
750 if (stuffing_size > 16) {
751 pad_packet_bytes += stuffing_size;
752 packet_size -= stuffing_size;
753 payload_size -= stuffing_size;
757 nb_frames=
get_nb_frames(ctx, stream, payload_size - stuffing_size);
764 for(i=0;i<stuffing_size;i++)
786 avio_w8(ctx->
pb, header_len - 3 + stuffing_size);
788 if (pes_flags & 0x80)
790 if (pes_flags & 0x40)
793 if (pes_flags & 0x01) {
821 for(i=0;i<stuffing_size;i++)
834 }
else if (
id >= 0x40) {
850 if (pad_packet_bytes > 0)
853 for(i=0;i<zero_trail_bytes;i++)
866 return payload_size - stuffing_size;
904 && scr > pkt_desc->
dts){
908 "buffer underflow i=%d bufi=%d size=%d\n",
926 int i, avail_space=0, es_size, trailer_size;
928 int best_score= INT_MIN;
929 int ignore_constraints=0;
950 assert(avail_data>0);
952 if(space < s->packet_size && !ignore_constraints)
955 if(next_pkt && next_pkt->
dts - scr > max_delay)
958 if(rel_space > best_score){
959 best_score= rel_space;
966 int64_t best_dts= INT64_MAX;
972 if(pkt_desc && pkt_desc->
dts < best_dts)
973 best_dts= pkt_desc->
dts;
976 av_dlog(ctx,
"bumping scr, scr:%f, dts:%f\n",
977 scr / 90000.0, best_dts / 90000.0);
978 if(best_dts == INT64_MAX)
981 if(scr >= best_dts+1 && !ignore_constraints){
983 ignore_constraints= 1;
985 scr=
FFMAX(best_dts+1, scr);
998 assert(avail_space >= s->
packet_size || ignore_constraints);
1005 timestamp_packet= timestamp_packet->
next;
1008 if(timestamp_packet){
1009 av_dlog(ctx,
"dts:%f pts:%f scr:%f stream:%d\n",
1010 timestamp_packet->
dts / 90000.0,
1011 timestamp_packet->
pts / 90000.0,
1012 scr / 90000.0, best_i);
1013 es_size=
flush_packet(ctx, best_i, timestamp_packet->
pts, timestamp_packet->
dts, scr, trailer_size);
1080 av_dlog(ctx,
"dts:%f pts:%f flags:%d stream:%d nopts:%d\n",
1081 dts / 90000.0, pts / 90000.0, pkt->
flags,
1144 #define OFFSET(x) offsetof(MpegMuxContext, x)
1145 #define E AV_OPT_FLAG_ENCODING_PARAM
1148 {
"preload",
"Initial demux-decode delay in microseconds.",
OFFSET(preload),
AV_OPT_TYPE_INT, {.i64 = 500000}, 0, INT_MAX,
E},
1152 #define MPEGENC_CLASS(flavor)\
1153 static const AVClass flavor ## _class = {\
1154 .class_name = #flavor " muxer",\
1155 .item_name = av_default_item_name,\
1156 .version = LIBAVUTIL_VERSION_INT,\
1160 #if CONFIG_MPEG1SYSTEM_MUXER
1165 .mime_type =
"video/mpeg",
1166 .extensions =
"mpg,mpeg",
1173 .priv_class = &mpeg_class,
1176 #if CONFIG_MPEG1VCD_MUXER
1181 .mime_type =
"video/mpeg",
1188 .priv_class = &vcd_class,
1191 #if CONFIG_MPEG2VOB_MUXER
1196 .mime_type =
"video/mpeg",
1197 .extensions =
"vob",
1204 .priv_class = &vob_class,
1209 #if CONFIG_MPEG2SVCD_MUXER
1214 .mime_type =
"video/mpeg",
1215 .extensions =
"vob",
1222 .priv_class = &svcd_class,
1227 #if CONFIG_MPEG2DVD_MUXER
1232 .mime_type =
"video/mpeg",
1233 .extensions =
"dvd",
1240 .priv_class = &dvd_class,