31 #define MAX_PAYLOAD_SIZE 4096
54 uint8_t lpcm_header[3];
90 uint8_t *buf, int64_t timestamp)
103 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
105 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
107 put_bits(&pb, 15, (uint32_t)((timestamp ) & 0x7fff));
128 int size, i, private_stream_coded,
id;
184 int P_STD_max_video = 0;
185 int P_STD_max_mpeg_audio = 0;
186 int P_STD_max_mpeg_PS1 = 0;
194 }
else if (
id >= 0xc0 && id <= 0xc7 && stream->max_buffer_size > P_STD_max_mpeg_audio) {
205 put_bits(&pb, 13, P_STD_max_video / 1024);
208 if (P_STD_max_mpeg_audio == 0)
209 P_STD_max_mpeg_audio = 4096;
213 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
219 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
229 private_stream_coded = 0;
237 if ( !s->
is_vcd || stream->
id==only_for_stream_id
238 || only_for_stream_id==0) {
243 if (private_stream_coded)
245 private_stream_coded = 1;
266 buf[4] = (size - 6) >> 8;
267 buf[5] = (size - 6) & 0xff;
274 int buf_index, i, private_stream_coded;
282 private_stream_coded = 0;
285 if (stream->
id < 0xc0) {
286 if (private_stream_coded)
288 private_stream_coded = 1;
298 int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j;
345 stream->
id = ac3_id++;
347 stream->
id = dts_id++;
349 stream->
id = lpcm_id++;
350 for(j = 0; j < 4; j++) {
363 stream->
id = mpa_id++;
372 stream->
id = mpv_id++;
390 stream->
id = mps_id++;
416 bitrate += codec_rate;
419 audio_bitrate += codec_rate;
421 video_bitrate += codec_rate;
426 s->
mux_rate= (ctx->mux_rate + (8 * 50) - 1) / (8 * 50);
432 bitrate += bitrate / 20;
434 s->
mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
438 double overhead_rate;
458 overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
459 overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
506 (((timestamp >> 30) & 0x07) << 1) |
508 avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
509 avio_wb16(pb, (uint16_t)((((timestamp ) & 0x7fff) << 1) | 1));
522 int64_t full_pad_bytes;
541 static int get_packet_payload_size(
AVFormatContext *ctx,
int stream_index,
542 int64_t pts, int64_t dts)
600 if (stream->
id < 0xc0) {
603 if (stream->
id >= 0xa0) {
637 for(i=0;i<packet_bytes;i++)
649 pkt_desc= pkt_desc->
next;
657 int64_t pts, int64_t dts, int64_t scr,
int trailer_size)
662 int size, payload_size, startcode,
id, stuffing_size, i, header_len;
665 int zero_trail_bytes = 0;
666 int pad_packet_bytes = 0;
668 int general_pack = 0;
673 av_dlog(ctx,
"packet ID=%2x PTS=%0.3f\n",
id, pts / 90000.0);
694 int PES_bytes_to_fill = s->
packet_size - size - 10;
698 PES_bytes_to_fill -= 5 + 5;
700 PES_bytes_to_fill -= 5;
712 for (i = 0; i < 979; i++)
718 for (i = 0; i < 1017; i++)
721 memset(buffer, 0, 128);
749 zero_trail_bytes += 20;
761 pad_packet_bytes = packet_size - zero_trail_bytes;
764 packet_size -= pad_packet_bytes + zero_trail_bytes;
766 if (packet_size > 0) {
790 payload_size = packet_size - header_len;
800 startcode = 0x100 +
id;
811 timestamp_len += s->
is_mpeg2 ? 5 : 4;
813 header_len -= timestamp_len;
815 pad_packet_bytes += timestamp_len;
816 packet_size -= timestamp_len;
818 payload_size += timestamp_len;
820 stuffing_size += timestamp_len;
821 if(payload_size > trailer_size)
822 stuffing_size += payload_size - trailer_size;
825 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) {
826 packet_size += pad_packet_bytes;
827 payload_size += pad_packet_bytes;
828 if (stuffing_size < 0) {
829 stuffing_size = pad_packet_bytes;
831 stuffing_size += pad_packet_bytes;
833 pad_packet_bytes = 0;
836 if (stuffing_size < 0)
838 if (stuffing_size > 16) {
839 pad_packet_bytes += stuffing_size;
840 packet_size -= stuffing_size;
841 payload_size -= stuffing_size;
845 nb_frames=
get_nb_frames(ctx, stream, payload_size - stuffing_size);
852 for(i=0;i<stuffing_size;i++)
874 avio_w8(ctx->
pb, header_len - 3 + stuffing_size);
876 if (pes_flags & 0x80)
878 if (pes_flags & 0x40)
881 if (pes_flags & 0x01) {
909 for(i=0;i<stuffing_size;i++)
922 }
else if (
id >= 0x40) {
938 if (pad_packet_bytes > 0)
941 for(i=0;i<zero_trail_bytes;i++)
954 return payload_size - stuffing_size;
983 static int64_t get_vcd_scr(
AVFormatContext *ctx,
int stream_index,int64_t pts)
1014 && scr > pkt_desc->
dts){
1018 "buffer underflow i=%d bufi=%d size=%d\n",
1036 int i, avail_space=0, es_size, trailer_size;
1038 int best_score= INT_MIN;
1039 int ignore_constraints=0;
1060 assert(avail_data>0);
1062 if(space < s->packet_size && !ignore_constraints)
1065 if(next_pkt && next_pkt->
dts - scr > max_delay)
1068 if(rel_space > best_score){
1069 best_score= rel_space;
1082 if(pkt_desc && pkt_desc->
dts < best_dts)
1083 best_dts= pkt_desc->
dts;
1086 av_dlog(ctx,
"bumping scr, scr:%f, dts:%f\n",
1087 scr / 90000.0, best_dts / 90000.0);
1091 if(scr >= best_dts+1 && !ignore_constraints){
1093 ignore_constraints= 1;
1095 scr=
FFMAX(best_dts+1, scr);
1101 assert(best_i >= 0);
1108 assert(avail_space >= s->
packet_size || ignore_constraints);
1115 timestamp_packet= timestamp_packet->
next;
1118 if(timestamp_packet){
1120 es_size=
flush_packet(ctx, best_i, timestamp_packet->
pts, timestamp_packet->
dts, scr, trailer_size);
1158 uint8_t *buf= pkt->
data;
1244 #define OFFSET(x) offsetof(MpegMuxContext, x)
1245 #define E AV_OPT_FLAG_ENCODING_PARAM
1248 {
"preload",
"Initial demux-decode delay in microseconds.",
OFFSET(preload),
AV_OPT_TYPE_INT, {500000}, 0, INT_MAX,
E},
1252 #define MPEGENC_CLASS(flavor)\
1253 static const AVClass flavor ## _class = {\
1254 .class_name = #flavor " muxer",\
1255 .item_name = av_default_item_name,\
1256 .version = LIBAVUTIL_VERSION_INT,\
1260 #if CONFIG_MPEG1SYSTEM_MUXER
1265 .mime_type =
"video/mpeg",
1266 .extensions =
"mpg,mpeg",
1273 .priv_class = &mpeg_class,
1276 #if CONFIG_MPEG1VCD_MUXER
1281 .mime_type =
"video/mpeg",
1288 .priv_class = &vcd_class,
1291 #if CONFIG_MPEG2VOB_MUXER
1296 .mime_type =
"video/mpeg",
1297 .extensions =
"vob",
1304 .priv_class = &vob_class,
1309 #if CONFIG_MPEG2SVCD_MUXER
1314 .mime_type =
"video/mpeg",
1315 .extensions =
"vob",
1322 .priv_class = &svcd_class,
1327 #if CONFIG_MPEG2DVD_MUXER
1332 .mime_type =
"video/mpeg",
1333 .extensions =
"dvd",
1340 .priv_class = &dvd_class,
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
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
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.
static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr)
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 int get_system_header_size(AVFormatContext *ctx)
#define AV_LOG_WARNING
Something somehow does not look correct.
static const int lpcm_freq_tab[4]
static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len)
struct PacketDesc PacketDesc
int64_t vcd_padding_bytes_written
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
double vcd_padding_bitrate
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
Resize 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 SYSTEM_HEADER_START_CODE
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.
PacketDesc * predecode_packet
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
preferred ID for MPEG-1/2 video decoding
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
#define MPEGENC_CLASS(flavor)
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 rc_max_rate
maximum bitrate
void av_log(void *avcl, int level, const char *fmt,...)
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.
AVCodecContext * codec
codec context
int rc_buffer_size
decoder bitstream buffer size
unsigned int nb_streams
A list of all streams in the file.
static void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
int bit_rate
the average bitrate
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.
AVOutputFormat ff_mpeg1vcd_muxer
static int mpeg_mux_init(AVFormatContext *ctx)
static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb, int packet_bytes)
#define av_dlog(pctx,...)
av_dlog macros Useful to print debug messages that shouldn't get compiled in normally.
void av_fifo_free(AVFifoBuffer *f)
Free an AVFifoBuffer.
enum AVMediaType codec_type
static void put_vcd_padding_sector(AVFormatContext *ctx)
static int mpeg_mux_end(AVFormatContext *ctx)
int sample_rate
samples per second
void avio_w8(AVIOContext *s, int b)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
a very simple circular buffer FIFO implementation
Describe the class of an AVClass context structure.
AVOutputFormat ff_mpeg2dvd_muxer
PacketDesc * premux_packet
void avio_wb16(AVIOContext *s, unsigned int val)
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.
int channels
number of audio channels
void * priv_data
Format private data.
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)
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