34 #define MAX_PAYLOAD_SIZE 4096
107 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
109 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
111 put_bits(&pb, 15, (uint32_t)((timestamp ) & 0x7fff));
132 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 && stream->max_buffer_size > P_STD_max_mpeg_audio) {
209 put_bits(&pb, 13, P_STD_max_video / 1024);
212 if (P_STD_max_mpeg_audio == 0)
213 P_STD_max_mpeg_audio = 4096;
217 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
223 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
233 private_stream_coded = 0;
241 if ( !s->
is_vcd || stream->
id==only_for_stream_id
242 || only_for_stream_id==0) {
247 if (private_stream_coded)
249 private_stream_coded = 1;
277 int buf_index, i, private_stream_coded;
285 private_stream_coded = 0;
288 if (stream->
id < 0xc0) {
289 if (private_stream_coded)
291 private_stream_coded = 1;
301 int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j;
308 s->
is_vcd = (CONFIG_MPEG1VCD_MUXER && ctx->
oformat == &ff_mpeg1vcd_muxer);
309 s->
is_svcd = (CONFIG_MPEG2SVCD_MUXER && ctx->
oformat == &ff_mpeg2svcd_muxer);
310 s->
is_mpeg2 = ((CONFIG_MPEG2VOB_MUXER && ctx->
oformat == &ff_mpeg2vob_muxer) ||
311 (CONFIG_MPEG2DVD_MUXER && ctx->
oformat == &ff_mpeg2dvd_muxer) ||
312 (CONFIG_MPEG2SVCD_MUXER && ctx->
oformat == &ff_mpeg2svcd_muxer));
313 s->
is_dvd = (CONFIG_MPEG2DVD_MUXER && ctx->
oformat == &ff_mpeg2dvd_muxer);
354 "%s in MPEG-1 system streams is not widely supported, "
355 "consider using the vob or the dvd muxer "
356 "to force a MPEG-2 program stream.\n",
359 stream->
id = ac3_id++;
361 stream->
id = dts_id++;
363 stream->
id = lpcm_id++;
364 for(j = 0; j < 4; j++) {
377 stream->
id = mpa_id++;
386 stream->
id = mpv_id++;
391 "If you want the mpeg file to be compliant to some specification\n"
392 "Like DVD, VCD or others, make sure you set the correct buffer size\n");
402 stream->
id = mps_id++;
428 bitrate += codec_rate;
431 audio_bitrate += codec_rate;
433 video_bitrate += codec_rate;
441 bitrate += bitrate / 20;
443 s->
mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
451 double overhead_rate;
471 overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
472 overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
519 (((timestamp >> 30) & 0x07) << 1) |
521 avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
522 avio_wb16(pb, (uint16_t)((((timestamp ) & 0x7fff) << 1) | 1));
535 int64_t full_pad_bytes;
564 for(i=0;i<packet_bytes;i++)
576 pkt_desc= pkt_desc->
next;
584 int64_t pts, int64_t dts, int64_t scr,
int trailer_size)
589 int size, payload_size, startcode,
id, stuffing_size, i, header_len;
592 int zero_trail_bytes = 0;
593 int pad_packet_bytes = 0;
595 int general_pack = 0;
600 av_dlog(ctx,
"packet ID=%2x PTS=%0.3f\n",
id, pts / 90000.0);
621 int PES_bytes_to_fill = s->
packet_size - size - 10;
625 PES_bytes_to_fill -= 5 + 5;
627 PES_bytes_to_fill -= 5;
639 for (i = 0; i < 979; i++)
645 for (i = 0; i < 1017; i++)
648 memset(buffer, 0, 128);
676 zero_trail_bytes += 20;
688 pad_packet_bytes = packet_size - zero_trail_bytes;
691 packet_size -= pad_packet_bytes + zero_trail_bytes;
693 if (packet_size > 0) {
717 payload_size = packet_size - header_len;
727 startcode = 0x100 +
id;
738 timestamp_len += s->
is_mpeg2 ? 5 : 4;
740 header_len -= timestamp_len;
742 pad_packet_bytes += timestamp_len;
743 packet_size -= timestamp_len;
745 payload_size += timestamp_len;
747 stuffing_size += timestamp_len;
748 if(payload_size > trailer_size)
749 stuffing_size += payload_size - trailer_size;
752 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) {
753 packet_size += pad_packet_bytes;
754 payload_size += pad_packet_bytes;
755 if (stuffing_size < 0) {
756 stuffing_size = pad_packet_bytes;
758 stuffing_size += pad_packet_bytes;
760 pad_packet_bytes = 0;
763 if (stuffing_size < 0)
768 stuffing_size += payload_size % stream->
lpcm_align;
771 if (stuffing_size > 16) {
772 pad_packet_bytes += stuffing_size;
773 packet_size -= stuffing_size;
774 payload_size -= stuffing_size;
778 nb_frames=
get_nb_frames(ctx, stream, payload_size - stuffing_size);
785 for(i=0;i<stuffing_size;i++)
807 avio_w8(ctx->
pb, header_len - 3 + stuffing_size);
809 if (pes_flags & 0x80)
811 if (pes_flags & 0x40)
814 if (pes_flags & 0x01) {
842 for(i=0;i<stuffing_size;i++)
855 }
else if (
id >= 0x40) {
871 if (pad_packet_bytes > 0)
874 for(i=0;i<zero_trail_bytes;i++)
887 return payload_size - stuffing_size;
925 && scr > pkt_desc->
dts){
929 "buffer underflow st=%d bufi=%d size=%d\n",
947 int i, avail_space=0, es_size, trailer_size;
949 int best_score= INT_MIN;
950 int ignore_constraints=0;
973 if(space < s->packet_size && !ignore_constraints)
976 if(next_pkt && next_pkt->
dts - scr > max_delay)
981 if(rel_space > best_score){
982 best_score= rel_space;
989 int64_t best_dts= INT64_MAX;
995 if(pkt_desc && pkt_desc->
dts < best_dts)
996 best_dts= pkt_desc->
dts;
999 av_dlog(ctx,
"bumping scr, scr:%f, dts:%f\n",
1000 scr / 90000.0, best_dts / 90000.0);
1001 if(best_dts == INT64_MAX)
1004 if(scr >= best_dts+1 && !ignore_constraints){
1006 ignore_constraints= 1;
1008 scr=
FFMAX(best_dts+1, scr);
1014 assert(best_i >= 0);
1021 assert(avail_space >= s->
packet_size || ignore_constraints);
1028 timestamp_packet= timestamp_packet->
next;
1031 if(timestamp_packet){
1032 av_dlog(ctx,
"dts:%f pts:%f scr:%f stream:%d\n",
1033 timestamp_packet->
dts / 90000.0,
1034 timestamp_packet->
pts / 90000.0,
1035 scr / 90000.0, best_i);
1036 es_size=
flush_packet(ctx, best_i, timestamp_packet->
pts, timestamp_packet->
dts, scr, trailer_size);
1103 av_dlog(ctx,
"dts:%f pts:%f flags:%d stream:%d nopts:%d\n",
1104 dts / 90000.0, pts / 90000.0, pkt->
flags,
1167 #define OFFSET(x) offsetof(MpegMuxContext, x)
1168 #define E AV_OPT_FLAG_ENCODING_PARAM
1171 {
"preload",
"Initial demux-decode delay in microseconds.",
OFFSET(preload),
AV_OPT_TYPE_INT, {.i64 = 500000}, 0, INT_MAX,
E},
1175 #define MPEGENC_CLASS(flavor)\
1176 static const AVClass flavor ## _class = {\
1177 .class_name = #flavor " muxer",\
1178 .item_name = av_default_item_name,\
1179 .version = LIBAVUTIL_VERSION_INT,\
1183 #if CONFIG_MPEG1SYSTEM_MUXER
1188 .mime_type =
"video/mpeg",
1189 .extensions =
"mpg,mpeg",
1196 .priv_class = &mpeg_class,
1199 #if CONFIG_MPEG1VCD_MUXER
1204 .mime_type =
"video/mpeg",
1211 .priv_class = &vcd_class,
1214 #if CONFIG_MPEG2VOB_MUXER
1219 .mime_type =
"video/mpeg",
1220 .extensions =
"vob",
1227 .priv_class = &vob_class,
1232 #if CONFIG_MPEG2SVCD_MUXER
1237 .mime_type =
"video/mpeg",
1238 .extensions =
"vob",
1245 .priv_class = &svcd_class,
1250 #if CONFIG_MPEG2DVD_MUXER
1255 .mime_type =
"video/mpeg",
1256 .extensions =
"dvd",
1263 .priv_class = &dvd_class,