32 #define MAX_PAYLOAD_SIZE 4096
105 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
107 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
109 put_bits(&pb, 15, (uint32_t)((timestamp ) & 0x7fff));
130 int size, i, private_stream_coded,
id;
186 int P_STD_max_video = 0;
187 int P_STD_max_mpeg_audio = 0;
188 int P_STD_max_mpeg_PS1 = 0;
196 }
else if (
id >= 0xc0 && id <= 0xc7 && stream->max_buffer_size > P_STD_max_mpeg_audio) {
207 put_bits(&pb, 13, P_STD_max_video / 1024);
210 if (P_STD_max_mpeg_audio == 0)
211 P_STD_max_mpeg_audio = 4096;
215 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
221 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
231 private_stream_coded = 0;
239 if ( !s->
is_vcd || stream->
id==only_for_stream_id
240 || only_for_stream_id==0) {
245 if (private_stream_coded)
247 private_stream_coded = 1;
268 buf[4] = (size - 6) >> 8;
269 buf[5] = (size - 6) & 0xff;
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, 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);
349 stream->
id = ac3_id++;
351 stream->
id = dts_id++;
353 stream->
id = lpcm_id++;
354 for(j = 0; j < 4; j++) {
367 stream->
id = mpa_id++;
376 stream->
id = mpv_id++;
386 stream->
id = mps_id++;
412 bitrate += codec_rate;
415 audio_bitrate += codec_rate;
417 video_bitrate += codec_rate;
425 bitrate += bitrate / 20;
427 s->
mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
431 double overhead_rate;
451 overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
452 overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
499 (((timestamp >> 30) & 0x07) << 1) |
501 avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
502 avio_wb16(pb, (uint16_t)((((timestamp ) & 0x7fff) << 1) | 1));
515 int64_t full_pad_bytes;
544 for(i=0;i<packet_bytes;i++)
556 pkt_desc= pkt_desc->
next;
564 int64_t pts, int64_t dts, int64_t scr,
int trailer_size)
569 int size, payload_size, startcode,
id, stuffing_size, i, header_len;
572 int zero_trail_bytes = 0;
573 int pad_packet_bytes = 0;
575 int general_pack = 0;
580 av_dlog(ctx,
"packet ID=%2x PTS=%0.3f\n",
id, pts / 90000.0);
601 int PES_bytes_to_fill = s->
packet_size - size - 10;
605 PES_bytes_to_fill -= 5 + 5;
607 PES_bytes_to_fill -= 5;
619 for (i = 0; i < 979; i++)
625 for (i = 0; i < 1017; i++)
628 memset(buffer, 0, 128);
656 zero_trail_bytes += 20;
668 pad_packet_bytes = packet_size - zero_trail_bytes;
671 packet_size -= pad_packet_bytes + zero_trail_bytes;
673 if (packet_size > 0) {
697 payload_size = packet_size - header_len;
707 startcode = 0x100 +
id;
718 timestamp_len += s->
is_mpeg2 ? 5 : 4;
720 header_len -= timestamp_len;
722 pad_packet_bytes += timestamp_len;
723 packet_size -= timestamp_len;
725 payload_size += timestamp_len;
727 stuffing_size += timestamp_len;
728 if(payload_size > trailer_size)
729 stuffing_size += payload_size - trailer_size;
732 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) {
733 packet_size += pad_packet_bytes;
734 payload_size += pad_packet_bytes;
735 if (stuffing_size < 0) {
736 stuffing_size = pad_packet_bytes;
738 stuffing_size += pad_packet_bytes;
740 pad_packet_bytes = 0;
743 if (stuffing_size < 0)
748 stuffing_size += payload_size % stream->
lpcm_align;
751 if (stuffing_size > 16) {
752 pad_packet_bytes += stuffing_size;
753 packet_size -= stuffing_size;
754 payload_size -= stuffing_size;
758 nb_frames=
get_nb_frames(ctx, stream, payload_size - stuffing_size);
765 for(i=0;i<stuffing_size;i++)
787 avio_w8(ctx->
pb, header_len - 3 + stuffing_size);
789 if (pes_flags & 0x80)
791 if (pes_flags & 0x40)
794 if (pes_flags & 0x01) {
822 for(i=0;i<stuffing_size;i++)
835 }
else if (
id >= 0x40) {
851 if (pad_packet_bytes > 0)
854 for(i=0;i<zero_trail_bytes;i++)
867 return payload_size - stuffing_size;
905 && scr > pkt_desc->
dts){
909 "buffer underflow i=%d bufi=%d size=%d\n",
927 int i, avail_space=0, es_size, trailer_size;
929 int best_score= INT_MIN;
930 int ignore_constraints=0;
951 assert(avail_data>0);
953 if(space < s->packet_size && !ignore_constraints)
956 if(next_pkt && next_pkt->
dts - scr > max_delay)
959 if(rel_space > best_score){
960 best_score= rel_space;
967 int64_t best_dts= INT64_MAX;
973 if(pkt_desc && pkt_desc->
dts < best_dts)
974 best_dts= pkt_desc->
dts;
977 av_dlog(ctx,
"bumping scr, scr:%f, dts:%f\n",
978 scr / 90000.0, best_dts / 90000.0);
979 if(best_dts == INT64_MAX)
982 if(scr >= best_dts+1 && !ignore_constraints){
984 ignore_constraints= 1;
986 scr=
FFMAX(best_dts+1, scr);
999 assert(avail_space >= s->
packet_size || ignore_constraints);
1006 timestamp_packet= timestamp_packet->
next;
1009 if(timestamp_packet){
1010 av_dlog(ctx,
"dts:%f pts:%f scr:%f stream:%d\n",
1011 timestamp_packet->
dts / 90000.0,
1012 timestamp_packet->
pts / 90000.0,
1013 scr / 90000.0, best_i);
1014 es_size=
flush_packet(ctx, best_i, timestamp_packet->
pts, timestamp_packet->
dts, scr, trailer_size);
1081 av_dlog(ctx,
"dts:%f pts:%f flags:%d stream:%d nopts:%d\n",
1082 dts / 90000.0, pts / 90000.0, pkt->
flags,
1145 #define OFFSET(x) offsetof(MpegMuxContext, x)
1146 #define E AV_OPT_FLAG_ENCODING_PARAM
1149 {
"preload",
"Initial demux-decode delay in microseconds.",
OFFSET(preload),
AV_OPT_TYPE_INT, {.i64 = 500000}, 0, INT_MAX,
E},
1153 #define MPEGENC_CLASS(flavor)\
1154 static const AVClass flavor ## _class = {\
1155 .class_name = #flavor " muxer",\
1156 .item_name = av_default_item_name,\
1157 .version = LIBAVUTIL_VERSION_INT,\
1161 #if CONFIG_MPEG1SYSTEM_MUXER
1166 .mime_type =
"video/mpeg",
1167 .extensions =
"mpg,mpeg",
1174 .priv_class = &mpeg_class,
1177 #if CONFIG_MPEG1VCD_MUXER
1182 .mime_type =
"video/mpeg",
1189 .priv_class = &vcd_class,
1192 #if CONFIG_MPEG2VOB_MUXER
1197 .mime_type =
"video/mpeg",
1198 .extensions =
"vob",
1205 .priv_class = &vob_class,
1210 #if CONFIG_MPEG2SVCD_MUXER
1215 .mime_type =
"video/mpeg",
1216 .extensions =
"vob",
1223 .priv_class = &svcd_class,
1228 #if CONFIG_MPEG2DVD_MUXER
1233 .mime_type =
"video/mpeg",
1234 .extensions =
"dvd",
1241 .priv_class = &dvd_class,