36 #define MAX_PAYLOAD_SIZE 4096 106 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
108 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
110 put_bits(&pb, 15, (uint32_t)((timestamp) & 0x7fff));
128 int only_for_stream_id)
131 int size, i, private_stream_coded,
id;
190 int P_STD_max_video = 0;
191 int P_STD_max_mpeg_audio = 0;
192 int P_STD_max_mpeg_PS1 = 0;
200 }
else if (
id >= 0xc0 &&
id <= 0xc7 &&
203 }
else if (
id == 0xe0 &&
213 put_bits(&pb, 13, P_STD_max_video / 1024);
216 if (P_STD_max_mpeg_audio == 0)
217 P_STD_max_mpeg_audio = 4096;
221 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
227 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
236 private_stream_coded = 0;
243 if (!s->
is_vcd || stream->
id == only_for_stream_id ||
244 only_for_stream_id == 0) {
248 if (private_stream_coded)
250 private_stream_coded = 1;
271 buf[4] = (size - 6) >> 8;
272 buf[5] = (size - 6) & 0xff;
279 int buf_index, i, private_stream_coded;
287 private_stream_coded = 0;
290 if (stream->
id < 0xc0) {
291 if (private_stream_coded)
293 private_stream_coded = 1;
303 int bitrate, i, mpa_id, mpv_id, h264_id, mps_id, ac3_id, dts_id, lpcm_id, j;
357 stream->
id = ac3_id++;
359 stream->
id = dts_id++;
361 stream->
id = lpcm_id++;
362 for (j = 0; j < 4; j++) {
375 stream->
id = mpa_id++;
385 stream->
id = h264_id++;
387 stream->
id = mpv_id++;
394 "VBV buffer size not set, muxing may fail\n");
401 stream->
id = mps_id++;
427 codec_rate = (1 << 21) * 8 * 50 / ctx->
nb_streams;
429 bitrate += codec_rate;
432 audio_bitrate += codec_rate;
434 video_bitrate += codec_rate;
440 bitrate += bitrate / 20;
442 s->
mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
446 double overhead_rate;
466 overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
467 overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
512 avio_w8(pb, (
id << 4) | (((timestamp >> 30) & 0x07) << 1) | 1);
513 avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
514 avio_wb16(pb, (uint16_t)((((timestamp) & 0x7fff) << 1) | 1));
525 int64_t full_pad_bytes;
556 for (i = 0; i < packet_bytes; i++)
569 pkt_desc = pkt_desc->
next;
577 int64_t
pts, int64_t
dts, int64_t scr,
int trailer_size)
582 int size, payload_size, startcode,
id, stuffing_size, i, header_len;
585 int zero_trail_bytes = 0;
586 int pad_packet_bytes = 0;
589 int general_pack = 0;
615 int PES_bytes_to_fill = s->
packet_size - size - 10;
619 PES_bytes_to_fill -= 5 + 5;
621 PES_bytes_to_fill -= 5;
633 for (i = 0; i < 979; i++)
639 for (i = 0; i < 1017; i++)
642 memset(buffer, 0, 128);
654 pad_packet_bytes = PES_bytes_to_fill -
673 zero_trail_bytes += 20;
686 pad_packet_bytes = packet_size - zero_trail_bytes;
689 packet_size -= pad_packet_bytes + zero_trail_bytes;
691 if (packet_size > 0) {
714 payload_size = packet_size - header_len;
724 startcode = 0x100 +
id;
731 int timestamp_len = 0;
735 timestamp_len += s->
is_mpeg2 ? 5 : 4;
738 header_len -= timestamp_len;
740 pad_packet_bytes += timestamp_len;
741 packet_size -= timestamp_len;
743 payload_size += timestamp_len;
745 stuffing_size += timestamp_len;
746 if (payload_size > trailer_size)
747 stuffing_size += payload_size - trailer_size;
751 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) {
752 packet_size += pad_packet_bytes;
753 payload_size += pad_packet_bytes;
754 if (stuffing_size < 0)
755 stuffing_size = pad_packet_bytes;
757 stuffing_size += pad_packet_bytes;
758 pad_packet_bytes = 0;
761 if (stuffing_size < 0)
766 stuffing_size += payload_size % stream->
lpcm_align;
769 if (stuffing_size > 16) {
770 pad_packet_bytes += stuffing_size;
771 packet_size -= stuffing_size;
772 payload_size -= stuffing_size;
776 nb_frames =
get_nb_frames(ctx, stream, payload_size - stuffing_size);
783 for (i = 0; i < stuffing_size; i++)
805 avio_w8(ctx->
pb, header_len - 3 + stuffing_size);
807 if (pes_flags & 0x80)
809 if (pes_flags & 0x40)
812 if (pes_flags & 0x01) {
839 for (i = 0; i < stuffing_size; i++)
852 }
else if (
id >= 0x40) {
862 payload_size - stuffing_size,
870 if (pad_packet_bytes > 0)
873 for (i = 0; i < zero_trail_bytes; i++)
886 return payload_size - stuffing_size;
924 scr > pkt_desc->
dts) {
928 "buffer underflow i=%d bufi=%d size=%d\n",
946 int i, avail_space = 0, es_size, trailer_size;
948 int best_score = INT_MIN;
949 int ignore_constraints = 0;
970 assert(avail_data > 0);
972 if (space < s->packet_size && !ignore_constraints)
975 if (next_pkt && next_pkt->
dts - scr > max_delay)
978 if (rel_space > best_score) {
979 best_score = rel_space;
986 int64_t best_dts = INT64_MAX;
992 if (pkt_desc && pkt_desc->
dts < best_dts)
993 best_dts = pkt_desc->
dts;
997 scr / 90000.0, best_dts / 90000.0);
998 if (best_dts == INT64_MAX)
1001 if (scr >= best_dts + 1 && !ignore_constraints) {
1003 "packet too large, ignoring buffer limits to mux it\n");
1004 ignore_constraints = 1;
1006 scr =
FFMAX(best_dts + 1, scr);
1012 assert(best_i >= 0);
1019 assert(avail_space >= s->
packet_size || ignore_constraints);
1026 timestamp_packet = timestamp_packet->
next;
1029 if (timestamp_packet) {
1031 timestamp_packet->
dts / 90000.0,
1032 timestamp_packet->
pts / 90000.0,
1033 scr / 90000.0, best_i);
1035 timestamp_packet->
dts, scr, trailer_size);
1101 dts / 90000.0, pts / 90000.0, pkt->
flags,
1166 #define OFFSET(x) offsetof(MpegMuxContext, x) 1167 #define E AV_OPT_FLAG_ENCODING_PARAM 1170 {
"preload",
"Initial demux-decode delay in microseconds.",
OFFSET(preload),
AV_OPT_TYPE_INT, { .i64 = 500000 }, 0, INT_MAX,
E },
1174 #define MPEGENC_CLASS(flavor) \ 1175 static const AVClass flavor ## _class = { \ 1176 .class_name = #flavor " muxer", \ 1177 .item_name = av_default_item_name, \ 1178 .version = LIBAVUTIL_VERSION_INT, \ 1179 .option = options, \ 1182 #if CONFIG_MPEG1SYSTEM_MUXER 1187 .mime_type =
"video/mpeg",
1188 .extensions =
"mpg,mpeg",
1195 .priv_class = &mpeg_class,
1199 #if CONFIG_MPEG1VCD_MUXER 1204 .mime_type =
"video/mpeg",
1211 .priv_class = &vcd_class,
1215 #if CONFIG_MPEG2VOB_MUXER 1220 .mime_type =
"video/mpeg",
1221 .extensions =
"vob",
1228 .priv_class = &vob_class,
1233 #if CONFIG_MPEG2SVCD_MUXER 1238 .mime_type =
"video/mpeg",
1239 .extensions =
"vob",
1246 .priv_class = &svcd_class,
1251 #if CONFIG_MPEG2DVD_MUXER 1256 .mime_type =
"video/mpeg",
1257 .extensions =
"dvd",
1264 .priv_class = &dvd_class,
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 av_cold int mpeg_mux_init(AVFormatContext *ctx)
#define CONFIG_MPEG1VCD_MUXER
#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).
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int64_t vcd_padding_bytes_written
uint8_t * av_stream_get_side_data(AVStream *stream, enum AVPacketSideDataType type, int *size)
Get side information from stream.
double vcd_padding_bitrate
#define CONFIG_MPEG2VOB_MUXER
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.
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
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)
AVOutputFormat ff_mpeg2svcd_muxer
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
AVStream ** streams
A list of all streams in the file.
#define SYSTEM_HEADER_START_CODE
PacketDesc * predecode_packet
void av_fifo_free(AVFifoBuffer *f)
Free an AVFifoBuffer.
int buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
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.
#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.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
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.
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
int flags
A combination of AV_PKT_FLAG values.
int bit_rate
The average bitrate of the encoded data (in bits per second).
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)
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.
#define CONFIG_MPEG2SVCD_MUXER
AVOutputFormat ff_mpeg1vcd_muxer
static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb, int packet_bytes)
preferred ID for MPEG-1/2 video decoding
if(ac->has_optimized_func)
This structure describes the bitrate properties of an encoded bitstream.
static void put_vcd_padding_sector(AVFormatContext *ctx)
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)
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
static void(WINAPI *cond_broadcast)(pthread_cond_t *cond)
a very simple circular buffer FIFO implementation
Describe the class of an AVClass context structure.
#define CONFIG_MPEG2DVD_MUXER
AVOutputFormat ff_mpeg2dvd_muxer
PacketDesc * premux_packet
void avio_wb16(AVIOContext *s, unsigned int val)
int sample_rate
Audio only.
int av_fifo_size(AVFifoBuffer *f)
Return the amount of data in bytes in the AVFifoBuffer, that is the amount of data you can read from ...
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static av_cold void flush(AVCodecContext *avctx)
Flush (reset) the frame ID after seeking.
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)
AVCodecParameters * codecpar
This structure stores compressed data.
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
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.
AVOutputFormat ff_mpeg2vob_muxer