36 #define MAX_PAYLOAD_SIZE 4096
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));
126 int only_for_stream_id)
129 int size,
i, private_stream_coded,
id;
141 if (
s->is_vcd && only_for_stream_id ==
VIDEO_ID) {
157 if (
s->is_vcd ||
s->is_dvd) {
168 if (
s->is_vcd && (only_for_stream_id & 0xe0) ==
AUDIO_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 &&
201 }
else if (
id == 0xe0 &&
211 put_bits(&pb, 13, P_STD_max_video / 1024);
214 if (P_STD_max_mpeg_audio == 0)
215 P_STD_max_mpeg_audio = 4096;
219 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
225 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
234 private_stream_coded = 0;
241 if (!
s->is_vcd || stream->
id == only_for_stream_id ||
242 only_for_stream_id == 0) {
246 if (private_stream_coded)
248 private_stream_coded = 1;
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, h264_id, mps_id, ac3_id, dts_id, lpcm_id, j;
306 s->packet_number = 0;
322 s->packet_size = 2048;
326 s->vcd_padding_bytes_written = 0;
327 s->vcd_padding_bitrate_num = 0;
359 "%s in MPEG-1 system streams is not widely supported, "
360 "consider using the vob or the dvd muxer "
361 "to force a MPEG-2 program stream.\n",
364 stream->
id = ac3_id++;
366 stream->
id = dts_id++;
368 stream->
id = lpcm_id++;
369 for (j = 0; j < 4; j++) {
377 for (sr = 0; sr < 4; sr++)
394 case 48000: freq = 0;
break;
395 case 96000: freq = 1;
break;
396 case 44100: freq = 2;
break;
397 case 32000: freq = 3;
break;
408 stream->
id = lpcm_id++;
418 av_log(
ctx,
AV_LOG_ERROR,
"Unsupported audio codec. Must be one of mp1, mp2, mp3, 16-bit pcm_dvd, pcm_s16be, ac3 or dts.\n");
421 stream->
id = mpa_id++;
431 stream->
id = h264_id++;
433 stream->
id = mpv_id++;
440 "VBV buffer size not set, using default size of 230KB\n"
441 "If you want the mpeg file to be compliant to some specification\n"
442 "Like DVD, VCD or others, make sure you set the correct buffer size\n");
453 stream->
id = mps_id++;
471 "Too many streams to fit the MPEG program stream system header\n");
496 audio_bitrate += codec_rate;
498 video_bitrate += codec_rate;
501 if (
s->user_mux_rate) {
502 s->mux_rate = (
s->user_mux_rate + (8 * 50) - 1) / (8 * 50);
508 s->mux_rate = (
bitrate + (8 * 50) - 1) / (8 * 50);
509 if (
s->mux_rate >= (1<<22)) {
511 s->mux_rate = (1<<22) - 1;
536 overhead_rate = audio_bitrate * 2294LL * (2324 - 2279);
537 overhead_rate += video_bitrate * 2279LL * (2324 - 2294);
540 s->vcd_padding_bitrate_num = (2324LL * 75 * 8 -
bitrate) * 2279 * 2294 - overhead_rate;
541 #define VCD_PADDING_BITRATE_DEN (2279 * 2294)
544 if (
s->is_vcd ||
s->is_mpeg2)
546 s->pack_header_freq = 1;
549 s->pack_header_freq = 2 *
bitrate /
s->packet_size / 8;
552 if (
s->pack_header_freq == 0)
553 s->pack_header_freq = 1;
557 s->system_header_freq =
s->pack_header_freq * 40;
562 s->system_header_freq = 0x7fffffff;
564 s->system_header_freq =
s->pack_header_freq * 5;
577 avio_w8(pb, (
id << 4) | (((timestamp >> 30) & 0x07) << 1) | 1);
578 avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
579 avio_wb16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
595 pad_bytes = (
int)(full_pad_bytes -
s->vcd_padding_bytes_written);
621 for (
i = 0;
i < packet_bytes;
i++)
634 pkt_desc = pkt_desc->
next;
647 int size, payload_size, startcode,
id, stuffing_size,
i, header_len;
651 int zero_trail_bytes = 0;
652 int pad_packet_bytes = 0;
655 int general_pack = 0;
664 if ((
s->packet_number %
s->pack_header_freq) == 0 ||
s->last_scr != scr) {
679 }
else if (
s->is_dvd) {
681 int PES_bytes_to_fill =
s->packet_size -
size - 10;
685 PES_bytes_to_fill -= 5 + 5;
687 PES_bytes_to_fill -= 5;
699 for (
i = 0;
i < 979;
i++)
705 for (
i = 0;
i < 1017;
i++)
713 scr +=
s->packet_size * 90000LL /
714 (
s->mux_rate * 50LL);
720 pad_packet_bytes = PES_bytes_to_fill -
725 if ((
s->packet_number %
s->system_header_freq) == 0) {
734 packet_size =
s->packet_size -
size;
736 if (
s->is_vcd && (
id & 0xe0) ==
AUDIO_ID)
739 zero_trail_bytes += 20;
742 (
s->is_svcd &&
s->packet_number == 0)) {
752 pad_packet_bytes = packet_size - zero_trail_bytes;
755 packet_size -= pad_packet_bytes + zero_trail_bytes;
757 if (packet_size > 0) {
780 payload_size = packet_size - header_len;
790 startcode = 0x100 +
id;
797 int timestamp_len = 0;
801 timestamp_len +=
s->is_mpeg2 ? 5 : 4;
804 header_len -= timestamp_len;
806 pad_packet_bytes += timestamp_len;
807 packet_size -= timestamp_len;
809 payload_size += timestamp_len;
811 stuffing_size += timestamp_len;
812 if (payload_size > trailer_size)
813 stuffing_size += payload_size - trailer_size;
817 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) {
818 packet_size += pad_packet_bytes;
819 payload_size += pad_packet_bytes;
820 if (stuffing_size < 0)
821 stuffing_size = pad_packet_bytes;
823 stuffing_size += pad_packet_bytes;
824 pad_packet_bytes = 0;
827 if (stuffing_size < 0)
832 stuffing_size += payload_size % stream->
lpcm_align;
835 if (stuffing_size > 16) {
836 pad_packet_bytes += stuffing_size;
837 packet_size -= stuffing_size;
838 payload_size -= stuffing_size;
849 for (
i = 0;
i < stuffing_size;
i++)
873 if (pes_flags & 0x80)
875 if (pes_flags & 0x40)
878 if (pes_flags & 0x01) {
905 for (
i = 0;
i < stuffing_size;
i++)
918 }
else if (
id >= 0x40) {
928 payload_size - stuffing_size,
936 if (pad_packet_bytes > 0)
939 for (
i = 0;
i < zero_trail_bytes;
i++)
952 return payload_size - stuffing_size;
966 for (
i = 0;
i <
s->packet_size;
i++)
969 s->vcd_padding_bytes_written +=
s->packet_size;
990 scr > pkt_desc->
dts) {
994 "buffer underflow st=%d bufi=%d size=%d\n",
1014 int i, avail_space = 0, es_size, trailer_size;
1016 int best_score = INT_MIN;
1017 int ignore_constraints = 0;
1018 int ignore_delay = 0;
1034 if (
s->packet_size > avail_data && !
flush
1037 if (avail_data == 0)
1041 if (space < s->packet_size && !ignore_constraints)
1044 if (next_pkt && next_pkt->
dts - scr > max_delay && !ignore_delay)
1049 if (rel_space > best_score) {
1050 best_score = rel_space;
1052 avail_space = space;
1064 if (pkt_desc && pkt_desc->
dts < best_dts)
1065 best_dts = pkt_desc->
dts;
1069 if (best_dts < INT64_MAX) {
1071 scr / 90000.0, best_dts / 90000.0);
1073 if (scr >= best_dts + 1 && !ignore_constraints) {
1075 "packet too large, ignoring buffer limits to mux it\n");
1076 ignore_constraints = 1;
1078 scr =
FFMAX(best_dts + 1, scr);
1081 }
else if (has_premux &&
flush) {
1083 "delay too large, ignoring ...\n");
1085 ignore_constraints = 1;
1099 av_assert0(avail_space >=
s->packet_size || ignore_constraints);
1106 timestamp_packet = timestamp_packet->
next;
1109 if (timestamp_packet) {
1111 timestamp_packet->
dts / 90000.0,
1112 timestamp_packet->
pts / 90000.0,
1113 scr / 90000.0, best_i);
1115 timestamp_packet->
dts, scr, trailer_size);
1131 s->last_scr +=
s->packet_size * 90000LL / (
s->mux_rate * 50LL);
1137 s->last_scr +=
s->packet_size * 90000LL / (
s->mux_rate * 50LL);
1175 if (dts ==
AV_NOPTS_VALUE || (dts < preload && ctx->avoid_negative_ts) ||
s->is_dvd) {
1180 s->last_scr = dts - preload;
1217 pkt_desc->
dts = dts;
1286 #define OFFSET(x) offsetof(MpegMuxContext, x)
1287 #define E AV_OPT_FLAG_ENCODING_PARAM
1290 {
"preload",
"Initial demux-decode delay in microseconds.",
OFFSET(preload),
AV_OPT_TYPE_INT, { .i64 = 500000 }, 0, INT_MAX,
E },
1294 #define MPEGENC_CLASS(flavor) \
1295 static const AVClass flavor ## _class = { \
1296 .class_name = #flavor " muxer", \
1297 .item_name = av_default_item_name, \
1298 .version = LIBAVUTIL_VERSION_INT, \
1299 .option = options, \
1302 #if CONFIG_MPEG1SYSTEM_MUXER
1307 .mime_type =
"video/mpeg",
1308 .extensions =
"mpg,mpeg",
1316 .priv_class = &mpeg_class,
1320 #if CONFIG_MPEG1VCD_MUXER
1325 .mime_type =
"video/mpeg",
1333 .priv_class = &vcd_class,
1337 #if CONFIG_MPEG2VOB_MUXER
1342 .mime_type =
"video/mpeg",
1343 .extensions =
"vob",
1351 .priv_class = &vob_class,
1356 #if CONFIG_MPEG2SVCD_MUXER
1361 .mime_type =
"video/mpeg",
1362 .extensions =
"vob",
1370 .priv_class = &svcd_class,
1375 #if CONFIG_MPEG2DVD_MUXER
1380 .mime_type =
"video/mpeg",
1381 .extensions =
"dvd",
1389 .priv_class = &dvd_class,
static void flush(AVCodecContext *avctx)
Macro definitions for various function/variable attributes.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
void avio_w8(AVIOContext *s, int b)
void avio_wb32(AVIOContext *s, unsigned int val)
void avio_wb16(AVIOContext *s, unsigned int val)
@ AVIO_DATA_MARKER_FLUSH_POINT
A point in the output bytestream where the underlying AVIOContext might flush the buffer depending on...
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
static void deinit(AVFormatContext *s)
#define CONFIG_MPEG1VCD_MUXER
#define CONFIG_MPEG2VOB_MUXER
#define CONFIG_MPEG2DVD_MUXER
#define CONFIG_MPEG2SVCD_MUXER
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
static void write_header(FFV1Context *f)
a very simple circular buffer FIFO implementation
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
@ AV_PKT_DATA_CPB_PROPERTIES
This side data corresponds to the AVCPBProperties struct.
uint8_t * av_stream_get_side_data(const AVStream *stream, enum AVPacketSideDataType type, size_t *size)
Get side information from stream.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_INFO
Standard information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
const char * av_get_media_type_string(enum AVMediaType media_type)
Return a string describing the media_type enum, NULL if media_type is unknown.
#define AV_NOPTS_VALUE
Undefined timestamp value.
#define AV_TIME_BASE
Internal time base represented as integer.
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
void av_fifo_freep(AVFifoBuffer **f)
Free an AVFifoBuffer and reset pointer to NULL.
int av_fifo_size(const AVFifoBuffer *f)
Return the amount of data in bytes in the AVFifoBuffer, that is the amount of data you can read from ...
int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void(*func)(void *, void *, int))
Feed data from an AVFifoBuffer to a user-supplied callback.
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int(*func)(void *, void *, int))
Feed data from a user-supplied callback to an AVFifoBuffer.
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
Resize an AVFifoBuffer.
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
#define SYSTEM_HEADER_START_CODE
static const int lpcm_freq_tab[4]
AVOutputFormat ff_mpeg1vcd_muxer
static int get_system_header_size(AVFormatContext *ctx)
static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb, int packet_bytes)
#define MPEGENC_CLASS(flavor)
static const AVOption options[]
static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len)
static int mpeg_mux_end(AVFormatContext *ctx)
static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr)
AVOutputFormat ff_mpeg2vob_muxer
static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
AVOutputFormat ff_mpeg2svcd_muxer
static int put_system_header(AVFormatContext *ctx, uint8_t *buf, int buf_size, int only_for_stream_id)
static av_cold int mpeg_mux_init(AVFormatContext *ctx)
static int output_packet(AVFormatContext *ctx, int flush)
static void mpeg_mux_deinit(AVFormatContext *ctx)
AVOutputFormat ff_mpeg2dvd_muxer
#define VCD_PADDING_BITRATE_DEN
static void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
static int put_pack_header(AVFormatContext *ctx, uint8_t *buf, int64_t timestamp)
static int flush_packet(AVFormatContext *ctx, int stream_index, int64_t pts, int64_t dts, int64_t scr, int trailer_size)
static void put_vcd_padding_sector(AVFormatContext *ctx)
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
typedef void(RENAME(mix_any_func_type))
This structure describes the bitrate properties of an encoded bitstream.
int buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
int max_bitrate
Maximum bitrate of the stream, in bits per second.
Describe the class of an AVClass context structure.
int bits_per_coded_sample
The number of bits per sample in the codedwords.
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
enum AVMediaType codec_type
General type of the encoded data.
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
int sample_rate
Audio only.
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
AVIOContext * pb
I/O context.
ff_const59 struct AVOutputFormat * oformat
The output container format.
void * priv_data
Format private data.
AVStream ** streams
A list of all streams in the file.
This structure stores compressed data.
int flags
A combination of AV_PKT_FLAG values.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
AVCodecParameters * codecpar
Codec parameters associated with this stream.
int64_t vcd_padding_bytes_written
int64_t vcd_padding_bitrate_num
PacketDesc * premux_packet
PacketDesc * predecode_packet
static int write_trailer(AVFormatContext *s1)