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;
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;
310 (CONFIG_MPEG2DVD_MUXER && ctx->
oformat == &ff_mpeg2dvd_muxer) ||
358 "%s in MPEG-1 system streams is not widely supported, "
359 "consider using the vob or the dvd muxer "
360 "to force a MPEG-2 program stream.\n",
363 stream->
id = ac3_id++;
365 stream->
id = dts_id++;
367 stream->
id = lpcm_id++;
368 for (j = 0; j < 4; j++) {
381 stream->
id = mpa_id++;
391 stream->
id = h264_id++;
393 stream->
id = mpv_id++;
400 "VBV buffer size not set, using default size of 130KB\n"
401 "If you want the mpeg file to be compliant to some specification\n"
402 "Like DVD, VCD or others, make sure you set the correct buffer size\n");
413 stream->
id = mps_id++;
441 codec_rate = (1 << 21) * 8 * 50 / ctx->
nb_streams;
443 bitrate += codec_rate;
446 audio_bitrate += codec_rate;
448 video_bitrate += codec_rate;
456 bitrate += bitrate / 20;
458 s->
mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
466 int64_t overhead_rate;
486 overhead_rate = audio_bitrate * 2294LL * (2324 - 2279);
487 overhead_rate += video_bitrate * 2279LL * (2324 - 2294);
491 #define VCD_PADDING_BITRATE_DEN (2279 * 2294)
532 avio_w8(pb, (
id << 4) | (((timestamp >> 30) & 0x07) << 1) | 1);
533 avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
534 avio_wb16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
545 int64_t full_pad_bytes;
576 for (i = 0; i < packet_bytes; i++)
589 pkt_desc = pkt_desc->
next;
597 int64_t
pts, int64_t dts, int64_t scr,
int trailer_size)
602 int size, payload_size, startcode,
id, stuffing_size, i, header_len;
605 int zero_trail_bytes = 0;
606 int pad_packet_bytes = 0;
609 int general_pack = 0;
635 int PES_bytes_to_fill = s->
packet_size - size - 10;
639 PES_bytes_to_fill -= 5 + 5;
641 PES_bytes_to_fill -= 5;
653 for (i = 0; i < 979; i++)
659 for (i = 0; i < 1017; i++)
662 memset(buffer, 0, 128);
674 pad_packet_bytes = PES_bytes_to_fill -
693 zero_trail_bytes += 20;
706 pad_packet_bytes = packet_size - zero_trail_bytes;
709 packet_size -= pad_packet_bytes + zero_trail_bytes;
711 if (packet_size > 0) {
734 payload_size = packet_size - header_len;
744 startcode = 0x100 +
id;
751 int timestamp_len = 0;
755 timestamp_len += s->
is_mpeg2 ? 5 : 4;
758 header_len -= timestamp_len;
760 pad_packet_bytes += timestamp_len;
761 packet_size -= timestamp_len;
763 payload_size += timestamp_len;
765 stuffing_size += timestamp_len;
766 if (payload_size > trailer_size)
767 stuffing_size += payload_size - trailer_size;
771 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) {
772 packet_size += pad_packet_bytes;
773 payload_size += pad_packet_bytes;
774 if (stuffing_size < 0)
775 stuffing_size = pad_packet_bytes;
777 stuffing_size += pad_packet_bytes;
778 pad_packet_bytes = 0;
781 if (stuffing_size < 0)
786 stuffing_size += payload_size % stream->
lpcm_align;
789 if (stuffing_size > 16) {
790 pad_packet_bytes += stuffing_size;
791 packet_size -= stuffing_size;
792 payload_size -= stuffing_size;
796 nb_frames =
get_nb_frames(ctx, stream, payload_size - stuffing_size);
803 for (i = 0; i < stuffing_size; i++)
825 avio_w8(ctx->
pb, header_len - 3 + stuffing_size);
827 if (pes_flags & 0x80)
829 if (pes_flags & 0x40)
832 if (pes_flags & 0x01) {
859 for (i = 0; i < stuffing_size; i++)
872 }
else if (
id >= 0x40) {
882 payload_size - stuffing_size,
890 if (pad_packet_bytes > 0)
893 for (i = 0; i < zero_trail_bytes; i++)
906 return payload_size - stuffing_size;
944 scr > pkt_desc->
dts) {
948 "buffer underflow st=%d bufi=%d size=%d\n",
966 int i, avail_space = 0, es_size, trailer_size;
968 int best_score = INT_MIN;
969 int ignore_constraints = 0;
970 int ignore_delay = 0;
993 if (space < s->packet_size && !ignore_constraints)
996 if (next_pkt && next_pkt->
dts - scr > max_delay && !ignore_delay)
1001 if (rel_space > best_score) {
1002 best_score = rel_space;
1004 avail_space = space;
1009 int64_t best_dts = INT64_MAX;
1016 if (pkt_desc && pkt_desc->
dts < best_dts)
1017 best_dts = pkt_desc->
dts;
1021 if (best_dts < INT64_MAX) {
1023 scr / 90000.0, best_dts / 90000.0);
1025 if (scr >= best_dts + 1 && !ignore_constraints) {
1027 "packet too large, ignoring buffer limits to mux it\n");
1028 ignore_constraints = 1;
1030 scr =
FFMAX(best_dts + 1, scr);
1033 }
else if (has_premux && flush) {
1035 "delay too large, ignoring ...\n");
1037 ignore_constraints = 1;
1058 timestamp_packet = timestamp_packet->
next;
1061 if (timestamp_packet) {
1063 timestamp_packet->
dts / 90000.0,
1064 timestamp_packet->
pts / 90000.0,
1065 scr / 90000.0, best_i);
1067 timestamp_packet->
dts, scr, trailer_size);
1143 dts / 90000.0, pts / 90000.0, pkt->
flags,
1152 pkt_desc->
dts = dts;
1210 #define OFFSET(x) offsetof(MpegMuxContext, x)
1211 #define E AV_OPT_FLAG_ENCODING_PARAM
1214 {
"preload",
"Initial demux-decode delay in microseconds.",
OFFSET(preload),
AV_OPT_TYPE_INT, { .i64 = 500000 }, 0, INT_MAX,
E },
1218 #define MPEGENC_CLASS(flavor) \
1219 static const AVClass flavor ## _class = { \
1220 .class_name = #flavor " muxer", \
1221 .item_name = av_default_item_name, \
1222 .version = LIBAVUTIL_VERSION_INT, \
1223 .option = options, \
1226 #if CONFIG_MPEG1SYSTEM_MUXER
1231 .mime_type =
"video/mpeg",
1232 .extensions =
"mpg,mpeg",
1239 .priv_class = &mpeg_class,
1243 #if CONFIG_MPEG1VCD_MUXER
1248 .mime_type =
"video/mpeg",
1255 .priv_class = &vcd_class,
1259 #if CONFIG_MPEG2VOB_MUXER
1264 .mime_type =
"video/mpeg",
1265 .extensions =
"vob",
1272 .priv_class = &vob_class,
1277 #if CONFIG_MPEG2SVCD_MUXER
1282 .mime_type =
"video/mpeg",
1283 .extensions =
"vob",
1290 .priv_class = &svcd_class,
1295 #if CONFIG_MPEG2DVD_MUXER
1300 .mime_type =
"video/mpeg",
1301 .extensions =
"dvd",
1308 .priv_class = &dvd_class,
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
static int put_pack_header(AVFormatContext *ctx, uint8_t *buf, int64_t timestamp)
static const AVOption options[]
PacketDesc ** next_packet
static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr)
static void flush(AVCodecContext *avctx)
static av_cold int mpeg_mux_init(AVFormatContext *ctx)
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
#define AV_LOG_WARNING
Something somehow does not look correct.
static int get_system_header_size(AVFormatContext *ctx)
int max_bitrate
Maximum bitrate of the stream, in bits per second.
static const int lpcm_freq_tab[4]
static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len)
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
int64_t vcd_padding_bytes_written
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Macro definitions for various function/variable attributes.
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.
static int output_packet(AVFormatContext *ctx, int flush)
static int flush_packet(AVFormatContext *ctx, int stream_index, int64_t pts, int64_t dts, int64_t scr, int trailer_size)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
AVOutputFormat ff_mpeg2svcd_muxer
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
uint8_t * av_stream_get_side_data(const AVStream *stream, enum AVPacketSideDataType type, int *size)
Get side information from stream.
AVStream ** streams
A list of all streams in the file.
#define SYSTEM_HEADER_START_CODE
PacketDesc * predecode_packet
int buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
int64_t vcd_padding_bitrate_num
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
struct AVOutputFormat * oformat
The output container format.
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define MPEGENC_CLASS(flavor)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static uint8_t * put_bits_ptr(PutBitContext *s)
Return the pointer to the byte where the bitstream writer will put the next bit.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
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.
enum AVMediaType codec_type
General type of the encoded data.
int flags
A combination of AV_PKT_FLAG values.
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
static void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
#define AV_TIME_BASE
Internal time base represented as integer.
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
AVOutputFormat ff_mpeg1vcd_muxer
static int write_trailer(AVFormatContext *s1)
static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb, int packet_bytes)
preferred ID for MPEG-1/2 video decoding
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
This structure describes the bitrate properties of an encoded bitstream.
static void put_vcd_padding_sector(AVFormatContext *ctx)
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 ...
static int mpeg_mux_end(AVFormatContext *ctx)
AVIOContext * pb
I/O context.
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
Resize an AVFifoBuffer.
void avio_w8(AVIOContext *s, int b)
a very simple circular buffer FIFO implementation
Describe the class of an AVClass context structure.
AVOutputFormat ff_mpeg2dvd_muxer
PacketDesc * premux_packet
static int64_t pts
Global timestamp for the audio frames.
void avio_wb16(AVIOContext *s, unsigned int val)
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.
int sample_rate
Audio only.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
void * priv_data
Format private data.
This side data corresponds to the AVCPBProperties struct.
static void write_header(FFV1Context *f)
static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
void avio_wb32(AVIOContext *s, unsigned int val)
static int put_system_header(AVFormatContext *ctx, uint8_t *buf, int only_for_stream_id)
void av_fifo_freep(AVFifoBuffer **f)
Free an AVFifoBuffer and reset pointer to NULL.
AVCodecParameters * codecpar
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define AV_NOPTS_VALUE
Undefined timestamp value.
#define VCD_PADDING_BITRATE_DEN
AVOutputFormat ff_mpeg2vob_muxer