Go to the documentation of this file.
22 #include "config_components.h"
41 #define MAX_PAYLOAD_SIZE 4096
109 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
111 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
113 put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
131 int only_for_stream_id)
134 int size,
i, private_stream_coded,
id;
146 if (
s->is_vcd && only_for_stream_id ==
VIDEO_ID) {
162 if (
s->is_vcd ||
s->is_dvd) {
173 if (
s->is_vcd && (only_for_stream_id & 0xe0) ==
AUDIO_ID) {
193 int P_STD_max_video = 0;
194 int P_STD_max_mpeg_audio = 0;
195 int P_STD_max_mpeg_PS1 = 0;
203 }
else if (
id >= 0xc0 &&
id <= 0xc7 &&
206 }
else if (
id == 0xe0 &&
216 put_bits(&pb, 13, P_STD_max_video / 1024);
219 if (P_STD_max_mpeg_audio == 0)
220 P_STD_max_mpeg_audio = 4096;
224 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
230 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
239 private_stream_coded = 0;
246 if (!
s->is_vcd || stream->
id == only_for_stream_id ||
247 only_for_stream_id == 0) {
251 if (private_stream_coded)
253 private_stream_coded = 1;
281 int buf_index,
i, private_stream_coded;
289 private_stream_coded = 0;
292 if (stream->
id < 0xc0) {
293 if (private_stream_coded)
295 private_stream_coded = 1;
305 int bitrate,
i, mpa_id, mpv_id, h264_id, mps_id, ac3_id, dts_id, lpcm_id, j;
311 s->packet_number = 0;
327 s->packet_size = 2048;
331 s->vcd_padding_bytes_written = 0;
332 s->vcd_padding_bitrate_num = 0;
362 "%s in MPEG-1 system streams is not widely supported, "
363 "consider using the vob or the dvd muxer "
364 "to force a MPEG-2 program stream.\n",
367 stream->
id = ac3_id++;
369 stream->
id = dts_id++;
371 stream->
id = lpcm_id++;
372 for (j = 0; j < 4; j++) {
380 for (sr = 0; sr < 4; sr++)
397 case 48000: freq = 0;
break;
398 case 96000: freq = 1;
break;
399 case 44100: freq = 2;
break;
400 case 32000: freq = 3;
break;
411 stream->
id = lpcm_id++;
421 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");
424 stream->
id = mpa_id++;
436 stream->
id = h264_id++;
438 stream->
id = mpv_id++;
449 "VBV buffer size not set, using default size of 230KB\n"
450 "If you want the mpeg file to be compliant to some specification\n"
451 "Like DVD, VCD or others, make sure you set the correct buffer size\n");
463 stream->
id = mps_id++;
501 audio_bitrate += codec_rate;
503 video_bitrate += codec_rate;
506 if (
s->user_mux_rate) {
507 s->mux_rate = (
s->user_mux_rate + (8 * 50) - 1) / (8 * 50);
513 s->mux_rate = (
bitrate + (8 * 50) - 1) / (8 * 50);
514 if (
s->mux_rate >= (1<<22)) {
516 s->mux_rate = (1<<22) - 1;
541 overhead_rate = audio_bitrate * 2294LL * (2324 - 2279);
542 overhead_rate += video_bitrate * 2279LL * (2324 - 2294);
545 s->vcd_padding_bitrate_num = (2324LL * 75 * 8 -
bitrate) * 2279 * 2294 - overhead_rate;
546 #define VCD_PADDING_BITRATE_DEN (2279 * 2294)
549 if (
s->is_vcd ||
s->is_mpeg2)
551 s->pack_header_freq = 1;
554 s->pack_header_freq = 2 *
bitrate /
s->packet_size / 8;
557 if (
s->pack_header_freq == 0)
558 s->pack_header_freq = 1;
562 s->system_header_freq =
s->pack_header_freq * 40;
567 s->system_header_freq = 0x7fffffff;
569 s->system_header_freq =
s->pack_header_freq * 5;
582 avio_w8(pb, (
id << 4) | (((timestamp >> 30) & 0x07) << 1) | 1);
583 avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
584 avio_wb16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
600 pad_bytes = (
int)(full_pad_bytes -
s->vcd_padding_bytes_written);
637 pkt_desc = pkt_desc->
next;
656 int size, payload_size, startcode,
id, stuffing_size, header_len;
659 int zero_trail_bytes = 0;
660 int pad_packet_bytes = 0;
663 int general_pack = 0;
672 if ((
s->packet_number %
s->pack_header_freq) == 0 ||
s->last_scr != scr) {
687 }
else if (
s->is_dvd) {
689 int PES_bytes_to_fill =
s->packet_size -
size - 10;
693 PES_bytes_to_fill -= 5 + 5;
695 PES_bytes_to_fill -= 5;
719 scr +=
s->packet_size * 90000LL /
720 (
s->mux_rate * 50LL);
726 pad_packet_bytes = PES_bytes_to_fill -
731 if ((
s->packet_number %
s->system_header_freq) == 0) {
740 packet_size =
s->packet_size -
size;
742 if (
s->is_vcd && (
id & 0xe0) ==
AUDIO_ID)
745 zero_trail_bytes += 20;
748 (
s->is_svcd &&
s->packet_number == 0)) {
758 pad_packet_bytes = packet_size - zero_trail_bytes;
761 packet_size -= pad_packet_bytes + zero_trail_bytes;
763 if (packet_size > 0) {
787 payload_size = packet_size - header_len;
797 startcode = 0x100 +
id;
804 int timestamp_len = 0;
808 timestamp_len +=
s->is_mpeg2 ? 5 : 4;
811 header_len -= timestamp_len;
813 pad_packet_bytes += timestamp_len;
814 packet_size -= timestamp_len;
816 payload_size += timestamp_len;
818 stuffing_size += timestamp_len;
819 if (payload_size > trailer_size)
820 stuffing_size += payload_size - trailer_size;
824 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) {
825 packet_size += pad_packet_bytes;
826 payload_size += pad_packet_bytes;
827 if (stuffing_size < 0)
828 stuffing_size = pad_packet_bytes;
830 stuffing_size += pad_packet_bytes;
831 pad_packet_bytes = 0;
834 if (stuffing_size < 0)
839 stuffing_size += payload_size % stream->
lpcm_align;
842 if (stuffing_size > 16) {
843 pad_packet_bytes += stuffing_size;
844 packet_size -= stuffing_size;
845 payload_size -= stuffing_size;
879 if (pes_flags & 0x80)
881 if (pes_flags & 0x40)
884 if (pes_flags & 0x01) {
923 }
else if (
id >= 0x40) {
931 fifo_data = payload_size - stuffing_size;
940 if (pad_packet_bytes > 0)
955 return payload_size - stuffing_size;
970 s->vcd_padding_bytes_written +=
s->packet_size;
991 scr > pkt_desc->
dts) {
995 "buffer underflow st=%d bufi=%d size=%d\n",
1015 int i, avail_space = 0, es_size, trailer_size;
1017 int best_score = INT_MIN;
1018 int ignore_constraints = 0;
1019 int ignore_delay = 0;
1035 if (
s->packet_size > avail_data && !
flush
1038 if (avail_data == 0)
1042 if (space < s->packet_size && !ignore_constraints)
1045 if (next_pkt && next_pkt->
dts - scr > max_delay && !ignore_delay)
1050 if (rel_space > best_score) {
1051 best_score = rel_space;
1053 avail_space =
space;
1065 if (pkt_desc && pkt_desc->
dts < best_dts)
1066 best_dts = pkt_desc->
dts;
1070 if (best_dts < INT64_MAX) {
1072 scr / 90000.0, best_dts / 90000.0);
1074 if (scr >= best_dts + 1 && !ignore_constraints) {
1076 "packet too large, ignoring buffer limits to mux it\n");
1077 ignore_constraints = 1;
1079 scr =
FFMAX(best_dts + 1, scr);
1082 }
else if (has_premux &&
flush) {
1084 "delay too large, ignoring ...\n");
1086 ignore_constraints = 1;
1100 av_assert0(avail_space >=
s->packet_size || ignore_constraints);
1107 timestamp_packet = timestamp_packet->
next;
1110 if (timestamp_packet) {
1112 timestamp_packet->
dts / 90000.0,
1113 timestamp_packet->
pts / 90000.0,
1114 scr / 90000.0, best_i);
1116 timestamp_packet->
dts, scr, trailer_size);
1132 s->last_scr +=
s->packet_size * 90000LL / (
s->mux_rate * 50LL);
1138 s->last_scr +=
s->packet_size * 90000LL / (
s->mux_rate * 50LL);
1160 const uint8_t *buf =
pkt->
data;
1177 if (dts ==
AV_NOPTS_VALUE || (dts < preload && ctx->avoid_negative_ts) ||
s->is_dvd) {
1182 s->last_scr = dts - preload;
1211 if (can_write <
size) {
1227 pkt_desc->
dts = dts;
1292 #define OFFSET(x) offsetof(MpegMuxContext, x)
1293 #define E AV_OPT_FLAG_ENCODING_PARAM
1295 {
"muxrate",
"mux rate as bits/s",
OFFSET(user_mux_rate),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, ((1<<22) - 1) * (8 * 50),
E },
1296 {
"preload",
"initial demux-decode delay in microseconds",
OFFSET(preload),
AV_OPT_TYPE_INT, { .i64 = 500000 }, 0, INT_MAX,
E },
1307 #if CONFIG_MPEG1SYSTEM_MUXER
1311 .p.mime_type =
"video/mpeg",
1312 .p.extensions =
"mpg,mpeg",
1324 #if CONFIG_MPEG1VCD_MUXER
1328 .p.mime_type =
"video/mpeg",
1340 #if CONFIG_MPEG2VOB_MUXER
1344 .p.mime_type =
"video/mpeg",
1345 .p.extensions =
"vob",
1358 #if CONFIG_MPEG2SVCD_MUXER
1362 .p.mime_type =
"video/mpeg",
1363 .p.extensions =
"vob",
1376 #if CONFIG_MPEG2DVD_MUXER
1380 .p.mime_type =
"video/mpeg",
1381 .p.extensions =
"dvd",
#define AV_LOG_WARNING
Something somehow does not look correct.
size_t av_fifo_can_write(const AVFifo *f)
#define SYSTEM_HEADER_START_CODE
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
enum AVMediaType codec_type
General type of the encoded data.
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated space
static int get_system_header_size(AVFormatContext *ctx)
#define VCD_PADDING_BITRATE_DEN
const FFOutputFormat ff_mpeg2dvd_muxer
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
static av_cold int mpeg_mux_init(AVFormatContext *ctx)
static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
AVStream ** streams
A list of all streams in the file.
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
static int flush_packet(AVFormatContext *ctx, int stream_index, int64_t pts, int64_t dts, int64_t scr, int trailer_size)
static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len)
int nb_channels
Number of channels in this layout.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
void avio_write_marker(AVIOContext *s, int64_t time, enum AVIODataMarkerType type)
Mark the written bytestream as a specific type.
int av_fifo_write(AVFifo *f, const void *buf, size_t nb_elems)
Write data into a FIFO.
static int output_packet(AVFormatContext *ctx, int flush)
int av_fifo_grow2(AVFifo *f, size_t inc)
Enlarge an AVFifo.
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
PacketDesc * predecode_packet
#define av_assert0(cond)
assert() equivalent, that is always enabled.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
static const int lpcm_freq_tab[4]
int nb_coded_side_data
Amount of entries in coded_side_data.
This structure describes the bitrate properties of an encoded bitstream.
AVCodecParameters * codecpar
Codec parameters associated with this stream.
#define LIBAVUTIL_VERSION_INT
Describe the class of an AVClass context structure.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
const char * av_default_item_name(void *ptr)
Return the context name.
AVIOContext * pb
I/O context.
size_t av_fifo_can_read(const AVFifo *f)
void(* flush)(AVBSFContext *ctx)
void avio_w8(AVIOContext *s, int b)
static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb, int packet_bytes)
void ffio_fill(AVIOContext *s, int b, int64_t count)
AVChannelLayout ch_layout
Audio only.
static void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
int sample_rate
Audio only.
const AVPacketSideData * av_packet_side_data_get(const AVPacketSideData *sd, int nb_sd, enum AVPacketSideDataType type)
Get side information from a side data array.
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
int av_fifo_read_to_cb(AVFifo *f, AVFifoCB write_cb, void *opaque, size_t *nb_elems)
Feed data from a FIFO into a user-provided callback.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
static void put_vcd_padding_sector(AVFormatContext *ctx)
#define AV_NOPTS_VALUE
Undefined timestamp value.
static const AVClass mpeg_class
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
void avio_wb32(AVIOContext *s, unsigned int val)
static int fifo_avio_wrapper(void *opaque, void *buf, size_t *nb_elems)
int flags
A combination of AV_PKT_FLAG values.
#define AV_LOG_INFO
Standard information.
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
@ AV_PKT_DATA_CPB_PROPERTIES
This side data corresponds to the AVCPBProperties struct.
PacketDesc * premux_packet
#define i(width, name, range_min, range_max)
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
static int mpeg_mux_end(AVFormatContext *ctx)
#define AV_TIME_BASE
Internal time base represented as integer.
int64_t max_bitrate
Maximum bitrate of the stream, in bits per second.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
const FFOutputFormat ff_mpeg2svcd_muxer
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
int64_t vcd_padding_bytes_written
AVPacketSideData * coded_side_data
Additional data associated with the entire stream.
const FFOutputFormat ff_mpeg1vcd_muxer
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
const struct AVOutputFormat * oformat
The output container format.
int64_t buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
AVFifo * av_fifo_alloc2(size_t nb_elems, size_t elem_size, unsigned int flags)
Allocate and initialize an AVFifo with a given element size.
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.
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
const FFOutputFormat ff_mpeg2vob_muxer
@ AV_OPT_TYPE_INT
Underlying C type is int.
int bits_per_coded_sample
The number of bits per sample in the codedwords.
static int put_system_header(AVFormatContext *ctx, uint8_t *buf, int only_for_stream_id)
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
int64_t vcd_padding_bitrate_num
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
This structure stores compressed data.
void avio_wb16(AVIOContext *s, unsigned int val)
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
void av_fifo_freep2(AVFifo **f)
Free an AVFifo and reset pointer to NULL.
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
static const AVOption options[]
void * priv_data
Format private data.
static void mpeg_mux_deinit(AVFormatContext *ctx)
static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
static int put_pack_header(AVFormatContext *ctx, uint8_t *buf, int64_t timestamp)
@ AVIO_DATA_MARKER_FLUSH_POINT
A point in the output bytestream where the underlying AVIOContext might flush the buffer depending on...