1 /*
2 * MPEG1/2 muxer
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdint.h>
23
29
31
35
36 #define MAX_PAYLOAD_SIZE 4096
37
45
61
70 int mux_rate;
/* bitrate in units of 50 bytes/s */
71 /* stream info */
79
82
85
90
92 int64_t timestamp)
93 {
96
98
102 else
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));
111 /* clock extension */
118 put_bits(&pb, 5, 0x1f);
/* reserved */
119 put_bits(&pb, 3, 0);
/* stuffing length */
120 }
123 }
124
126 int only_for_stream_id)
127 {
129 int size, i, private_stream_coded,
id;
131
133
137
138 /* maximum bit rate of the multiplexed stream */
142 /* This header applies only to the video stream
143 * (see VCD standard p. IV-7) */
145 } else
147
149 /* see VCD standard, p. IV-7 */
152 } else {
153 put_bits(&pb, 1, 0);
/* variable bitrate */
154 put_bits(&pb, 1, 0);
/* non constrainted bit stream */
155 }
156
158 /* see VCD standard p IV-7 */
159 put_bits(&pb, 1, 1);
/* audio locked */
160 put_bits(&pb, 1, 1);
/* video locked */
161 } else {
162 put_bits(&pb, 1, 0);
/* audio locked */
163 put_bits(&pb, 1, 0);
/* video locked */
164 }
165
167
169 /* This header applies only to the audio stream
170 * (see VCD standard p. IV-7) */
172 } else
174
176 put_bits(&pb, 1, 0);
/* packet_rate_restriction_flag */
177 put_bits(&pb, 7, 0x7f);
/* reserved byte */
178 } else
179 put_bits(&pb, 8, 0xff);
/* reserved byte */
180
181 /* DVD-Video Stream_bound entries
182 * id (0xB9) video, maximum P-STD for stream 0xE0. (P-STD_buffer_bound_scale = 1)
183 * id (0xB8) audio, maximum P-STD for any MPEG audio (0xC0 to 0xC7) streams. If there are none set to 4096 (32x128). (P-STD_buffer_bound_scale = 0)
184 * id (0xBD) private stream 1 (audio other than MPEG and subpictures). (P-STD_buffer_bound_scale = 1)
185 * id (0xBF) private stream 2, NAV packs, set to 2x1024. */
187
188 int P_STD_max_video = 0;
189 int P_STD_max_mpeg_audio = 0;
190 int P_STD_max_mpeg_PS1 = 0;
191
194
198 } else if (id >= 0xc0 && id <= 0xc7 &&
201 } else if (id == 0xe0 &&
204 }
205 }
206
207 /* video */
208 put_bits(&pb, 8, 0xb9);
/* stream ID */
211 put_bits(&pb, 13, P_STD_max_video / 1024);
212
213 /* audio */
214 if (P_STD_max_mpeg_audio == 0)
215 P_STD_max_mpeg_audio = 4096;
216 put_bits(&pb, 8, 0xb8);
/* stream ID */
219 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
220
221 /* private stream 1 */
222 put_bits(&pb, 8, 0xbd);
/* stream ID */
225 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
226
227 /* private stream 2 */
228 put_bits(&pb, 8, 0xbf);
/* stream ID */
232 } else {
233 /* audio stream info */
234 private_stream_coded = 0;
237
238 /* For VCDs, only include the stream info for the stream
239 * that the pack which contains this system belongs to.
240 * (see VCD standard p. IV-7) */
241 if (!s->
is_vcd || stream->
id == only_for_stream_id ||
242 only_for_stream_id == 0) {
244 if (id < 0xc0) {
245 /* special case for private streams (AC-3 uses that) */
246 if (private_stream_coded)
247 continue;
248 private_stream_coded = 1;
249 id = 0xbd;
250 }
251 put_bits(&pb, 8,
id);
/* stream ID */
253 if (id < 0xe0) {
254 /* audio */
257 } else {
258 /* video */
261 }
262 }
263 }
264 }
265
268 /* patch packet size */
270
272 }
273
275 {
276 int buf_index, i, private_stream_coded;
279
281 return 18; // DVD-Video system headers are 18 bytes fixed length.
282
283 buf_index = 12;
284 private_stream_coded = 0;
287 if (stream->
id < 0xc0) {
288 if (private_stream_coded)
289 continue;
290 private_stream_coded = 1;
291 }
292 buf_index += 3;
293 }
294 return buf_index;
295 }
296
298 {
300 int bitrate, i, mpa_id, mpv_id, h264_id, mps_id, ac3_id, dts_id, lpcm_id, j;
303 int audio_bitrate;
304 int video_bitrate;
305
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);
313
318 goto fail;
319 }
321 } else
323 if (ctx->
max_delay < 0)
/* Not set by the caller */
325
328
331
339
343 if (!stream)
344 goto fail;
346
348
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++) {
368 break;
369 }
370 if (j == 4)
371 goto fail;
373 return -1;
378 } else {
379 stream->
id = mpa_id++;
380 }
381
382 /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
383 * Right now it is also used for everything else. */
386 break;
389 stream->
id = h264_id++;
390 else
391 stream->
id = mpv_id++;
394 else {
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");
399 // FIXME: this is probably too small as default
401 }
405 }
407 break;
409 stream->
id = mps_id++;
411 break;
412 default:
413 return -1;
414 }
417 goto fail;
418 }
419 bitrate = 0;
420 audio_bitrate = 0;
421 video_bitrate = 0;
423 int codec_rate;
426
430 else
432
433 if (!codec_rate)
434 codec_rate = (1 << 21) * 8 * 50 / ctx->
nb_streams;
435
436 bitrate += codec_rate;
437
439 audio_bitrate += codec_rate;
441 video_bitrate += codec_rate;
442 }
443
446 } else {
447 /* we increase slightly the bitrate to take into account the
448 * headers. XXX: compute it exactly */
449 bitrate += bitrate / 20;
450 bitrate += 10000;
451 s->
mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
455 }
456 }
457
459 double overhead_rate;
460
461 /* The VCD standard mandates that the mux_rate field is 3528
462 * (see standard p. IV-6).
463 * The value is actually "wrong", i.e. if you calculate
464 * it using the normal formula and the 75 sectors per second transfer
465 * rate you get a different value because the real pack size is 2324,
466 * not 2352. But the standard explicitly specifies that the mux_rate
467 * field in the header must have this value. */
468 // s->mux_rate = 2352 * 75 / 50; /* = 3528 */
469
470 /* The VCD standard states that the muxed stream must be
471 * exactly 75 packs / second (the data rate of a single speed cdrom).
472 * Since the video bitrate (probably 1150000 bits/sec) will be below
473 * the theoretical maximum we have to add some padding packets
474 * to make up for the lower data rate.
475 * (cf. VCD standard p. IV-6 ) */
476
477 /* Add the header overhead to the data rate.
478 * 2279 data bytes per audio pack, 2294 data bytes per video pack */
479 overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
480 overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
481 overhead_rate *= 8;
482
483 /* Add padding so that the full bitrate is 2324*75 bytes/sec */
485 }
486
488 /* every packet */
490 else
491 /* every 2 seconds */
493
494 /* the above seems to make pack_header_freq zero sometimes */
497
499 /* every 200 packets. Need to look at the spec. */
502 /* the standard mandates that there are only two system headers
503 * in the whole file: one in the first packet of each stream.
504 * (see standard p. IV-7 and IV-8) */
506 else
508
512 }
515 return 0;
516
517 fail:
521 }
522
524 {
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));
528 }
529
530 /* return the number of padding bytes that should be inserted into
531 * the multiplexed stream. */
533 {
535 int pad_bytes = 0;
536
538 int64_t full_pad_bytes;
539
540 // FIXME: this is wrong
541 full_pad_bytes =
544
545 if (pad_bytes < 0)
546 /* might happen if we have already padded to a later timestamp. This
547 * can occur if another stream has already advanced further. */
548 pad_bytes = 0;
549 }
550
551 return pad_bytes;
552 }
553
554 /* Write an MPEG padding packet header. */
556 int packet_bytes)
557 {
559 int i;
560
565 packet_bytes -= 7;
566 } else
567 packet_bytes -= 6;
568
569 for (i = 0; i < packet_bytes; i++)
571 }
572
574 {
575 int nb_frames = 0;
577
578 while (len > 0) {
580 nb_frames++;
582 pkt_desc = pkt_desc->
next;
583 }
584
585 return nb_frames;
586 }
587
588 /* flush the packet on stream stream_index */
590 int64_t
pts, int64_t dts, int64_t scr,
int trailer_size)
591 {
595 int size, payload_size, startcode,
id, stuffing_size, i, header_len;
596 int packet_size;
598 int zero_trail_bytes = 0;
599 int pad_packet_bytes = 0;
600 int pes_flags;
601 /* "general" pack without data specific to one stream? */
602 int general_pack = 0;
603 int nb_frames;
604
606
607 av_dlog(ctx,
"packet ID=%2x PTS=%0.3f\n",
id, pts / 90000.0);
608
610
612 /* output pack and systems header if needed */
616
618 /* there is exactly one system header for each stream in a VCD MPEG,
619 * One in the very first video packet and one in the very first
620 * audio packet (see VCD standard p. IV-7 and IV-8). */
621
625 }
628 int PES_bytes_to_fill = s->
packet_size - size - 10;
629
631 if (dts != pts)
632 PES_bytes_to_fill -= 5 + 5;
633 else
634 PES_bytes_to_fill -= 5;
635 }
636
642
645 avio_w8(ctx->
pb, 0x00);
// substream ID, 00=PCI
646 for (i = 0; i < 979; i++)
648
651 avio_w8(ctx->
pb, 0x01);
// substream ID, 01=DSI
652 for (i = 0; i < 1017; i++)
654
655 memset(buffer, 0, 128);
659 // FIXME: rounding and first few bytes of each packet
665 /* GOP Start */
667 pad_packet_bytes = PES_bytes_to_fill -
669 }
670 }
671 } else {
675 }
676 }
677 }
680
682
684 /* The VCD standard demands that 20 zero bytes follow
685 * each audio pack (see standard p. IV-8). */
686 zero_trail_bytes += 20;
687
690 /* for VCD the first pack of each stream contains only the pack header,
691 * the system header and lots of padding (see VCD standard p. IV-6).
692 * In the case of an audio pack, 20 zero bytes are also added at
693 * the end. */
694 /* For SVCD we fill the very first pack to increase compatibility with
695 * some DVD players. Not mandated by the standard. */
697 /* the system header refers to both streams and no stream data */
698 general_pack = 1;
699 pad_packet_bytes = packet_size - zero_trail_bytes;
700 }
701
702 packet_size -= pad_packet_bytes + zero_trail_bytes;
703
704 if (packet_size > 0) {
705 /* packet header size */
706 packet_size -= 6;
707
708 /* packet header */
710 header_len = 3;
712 header_len += 3; /* PES extension */
713 header_len += 1; /* obligatory stuffing byte */
714 } else {
715 header_len = 0;
716 }
718 if (dts != pts)
719 header_len += 5 + 5;
720 else
721 header_len += 5;
722 } else {
724 header_len++;
725 }
726
727 payload_size = packet_size - header_len;
728 if (id < 0xc0) {
730 payload_size -= 1;
731 if (id >= 0x40) {
732 payload_size -= 3;
733 if (id >= 0xa0)
734 payload_size -= 3;
735 }
736 } else {
737 startcode = 0x100 +
id;
738 }
739
741
742 // first byte does not fit -> reset pts/dts + stuffing
744 int timestamp_len = 0;
745 if (dts != pts)
746 timestamp_len += 5;
748 timestamp_len += s->
is_mpeg2 ? 5 : 4;
749 pts =
751 header_len -= timestamp_len;
753 pad_packet_bytes += timestamp_len;
754 packet_size -= timestamp_len;
755 } else {
756 payload_size += timestamp_len;
757 }
758 stuffing_size += timestamp_len;
759 if (payload_size > trailer_size)
760 stuffing_size += payload_size - trailer_size;
761 }
762
763 // can't use padding, so use stuffing
764 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) {
765 packet_size += pad_packet_bytes;
766 payload_size += pad_packet_bytes; // undo the previous adjustment
767 if (stuffing_size < 0)
768 stuffing_size = pad_packet_bytes;
769 else
770 stuffing_size += pad_packet_bytes;
771 pad_packet_bytes = 0;
772 }
773
774 if (stuffing_size < 0)
775 stuffing_size = 0;
776
779 stuffing_size += payload_size % stream->
lpcm_align;
780 }
781
782 if (stuffing_size > 16) { /* <=16 for MPEG-1, <=32 for MPEG-2 */
783 pad_packet_bytes += stuffing_size;
784 packet_size -= stuffing_size;
785 payload_size -= stuffing_size;
786 stuffing_size = 0;
787 }
788
789 nb_frames =
get_nb_frames(ctx, stream, payload_size - stuffing_size);
790
792
794
796 for (i = 0; i < stuffing_size; i++)
798
801
802 pes_flags = 0;
803
805 pes_flags |= 0x80;
806 if (dts != pts)
807 pes_flags |= 0x40;
808 }
809
810 /* Both the MPEG-2 and the SVCD standards demand that the
811 * P-STD_buffer_size field be included in the first packet of
812 * every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
813 * and MPEG-2 standard 2.7.7) */
815 pes_flags |= 0x01;
816
818 avio_w8(ctx->
pb, header_len - 3 + stuffing_size);
819
820 if (pes_flags & 0x80) /* write pts */
822 if (pes_flags & 0x40) /* write dts */
824
825 if (pes_flags & 0x01) { /* write pes extension */
827
828 /* P-STD buffer info */
831 else
833 }
834 } else {
836 if (dts != pts) {
839 } else {
841 }
842 } else {
844 }
845 }
846
848 /* special stuffing byte that is always written
849 * to prevent accidental generation of start codes. */
851
852 for (i = 0; i < stuffing_size; i++)
854 }
855
858 if (id >= 0xa0) {
859 /* LPCM (XXX: check nb_frames) */
865 } else if (id >= 0x40) {
866 /* AC-3 */
869 }
870 }
871
872 /* output data */
875 payload_size - stuffing_size,
878 } else {
879 payload_size =
880 stuffing_size = 0;
881 }
882
883 if (pad_packet_bytes > 0)
885
886 for (i = 0; i < zero_trail_bytes; i++)
888
890
892
893 /* only increase the stream packet number if this pack actually contains
894 * something that is specific to this stream! I.e. a dedicated header
895 * or some data. */
896 if (!general_pack)
898
899 return payload_size - stuffing_size;
900 }
901
903 {
904 /* There are two ways to do this padding: writing a sector/pack
905 * of 0 values, or writing an MPEG padding pack. Both seem to
906 * work with most decoders, BUT the VCD standard only allows a 0-sector
907 * (see standard p. IV-4, IV-5).
908 * So a 0-sector it is... */
909
911 int i;
912
915
917
919
920 /* increasing the packet number is correct. The SCR of the following packs
921 * is calculated from the packet_number and it has to include the padding
922 * sector (it represents the sector index, not the MPEG pack index)
923 * (see VCD standard p. IV-6) */
925 }
926
928 {
929 int i;
930
935
937 scr > pkt_desc->
dts) {
// FIXME: > vs >=
941 "buffer underflow st=%d bufi=%d size=%d\n",
943 break;
944 }
948 }
949 }
950
951 return 0;
952 }
953
955 {
959 int i, avail_space = 0, es_size, trailer_size;
960 int best_i = -1;
961 int best_score = INT_MIN;
962 int ignore_constraints = 0;
963 int ignore_delay = 0;
967
968 retry:
976
977 /* for subtitle, a single PES packet must be generated,
978 * so we flush after every single subtitle packet */
981 return 0;
982 if (avail_data == 0)
983 continue;
985
986 if (space < s->packet_size && !ignore_constraints)
987 continue;
988
989 if (next_pkt && next_pkt->
dts - scr > max_delay && !ignore_delay)
990 continue;
993 rel_space += 1<<28;
994 if (rel_space > best_score) {
995 best_score = rel_space;
996 best_i = i;
997 avail_space = space;
998 }
999 }
1000
1001 if (best_i < 0) {
1002 int64_t best_dts = INT64_MAX;
1003 int has_premux = 0;
1004
1009 if (pkt_desc && pkt_desc->
dts < best_dts)
1010 best_dts = pkt_desc->
dts;
1012 }
1013
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);
1017
1018 if (scr >= best_dts + 1 && !ignore_constraints) {
1020 "packet too large, ignoring buffer limits to mux it\n");
1021 ignore_constraints = 1;
1022 }
1023 scr =
FFMAX(best_dts + 1, scr);
1025 return -1;
1026 } else if (has_premux && flush) {
1028 "delay too large, ignoring ...\n");
1029 ignore_delay = 1;
1030 ignore_constraints = 1;
1031 } else
1032 return 0;
1033
1034 goto retry;
1035 }
1036
1038
1041
1043
1045
1048 trailer_size = 0;
1049 } else {
1051 timestamp_packet = timestamp_packet->
next;
1052 }
1053
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);
1061 } else {
1064 trailer_size);
1065 }
1066
1068 /* Write one or more padding sectors, if necessary, to reach
1069 * the constant overall bitrate. */
1070 int vcd_pad_bytes;
1071
1072 // FIXME: pts cannot be correct here
1075 // FIXME: rounding and first few bytes of each packet
1077 }
1078 }
1079
1081 // FIXME: rounding and first few bytes of each packet
1083
1088 }
1089 if (es_size) {
1092 }
1093
1095 return -1;
1096
1097 return 1;
1098 }
1099
1101 {
1110 int preload;
1113
1115
1118
1124 } else {
1127 }
1130 }
1131
1134
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;
1149
1151 return -1;
1152
1154 // min VOBU length 0.4 seconds (mpucoder)
1155 if (is_iframe &&
1161 }
1162 }
1163
1165
1166 for (;;) {
1168 if (ret <= 0)
1170 }
1171 }
1172
1174 {
1176 int i;
1177
1178 for (;;) {
1180 if (ret < 0)
1182 else if (ret == 0)
1183 break;
1184 }
1185
1186 /* End header according to MPEG1 systems standard. We do not write
1187 * it as it is usually not needed by decoders and because it
1188 * complicates MPEG stream concatenation. */
1189 // avio_wb32(ctx->pb, ISO_11172_END_CODE);
1190 // avio_flush(ctx->pb);
1191
1194
1197 }
1198 return 0;
1199 }
1200
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 },
1207 };
1208
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, \
1215 };
1216
1217 #if CONFIG_MPEG1SYSTEM_MUXER
1222 .mime_type = "video/mpeg",
1223 .extensions = "mpg,mpeg",
1230 .priv_class = &mpeg_class,
1231 };
1232 #endif
1233
1234 #if CONFIG_MPEG1VCD_MUXER
1239 .mime_type = "video/mpeg",
1246 .priv_class = &vcd_class,
1247 };
1248 #endif
1249
1250 #if CONFIG_MPEG2VOB_MUXER
1255 .mime_type = "video/mpeg",
1256 .extensions = "vob",
1263 .priv_class = &vob_class,
1264 };
1265 #endif
1266
1267 /* Same as mpeg2vob_mux except that the pack size is 2324 */
1268 #if CONFIG_MPEG2SVCD_MUXER
1273 .mime_type = "video/mpeg",
1274 .extensions = "vob",
1281 .priv_class = &svcd_class,
1282 };
1283 #endif
1284
1285 /* Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
1286 #if CONFIG_MPEG2DVD_MUXER
1291 .mime_type = "video/mpeg",
1292 .extensions = "dvd",
1299 .priv_class = &dvd_class,
1300 };
1301 #endif