22 #include "libavutil/attributes.h"
23 #include "libavutil/fifo.h"
24 #include "libavutil/log.h"
25 #include "libavutil/mathematics.h"
26 #include "libavutil/opt.h"
32 #define MAX_PAYLOAD_SIZE 4096
105 put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07));
107 put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
109 put_bits(&pb, 15, (uint32_t)((timestamp ) & 0x7fff));
130 int size, i, private_stream_coded,
id;
186 int P_STD_max_video = 0;
187 int P_STD_max_mpeg_audio = 0;
188 int P_STD_max_mpeg_PS1 = 0;
196 }
else if (
id >= 0xc0 && id <= 0xc7 && stream->max_buffer_size > P_STD_max_mpeg_audio) {
207 put_bits(&pb, 13, P_STD_max_video / 1024);
210 if (P_STD_max_mpeg_audio == 0)
211 P_STD_max_mpeg_audio = 4096;
215 put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
221 put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
231 private_stream_coded = 0;
239 if ( !s->
is_vcd || stream->
id==only_for_stream_id
240 || only_for_stream_id==0) {
245 if (private_stream_coded)
247 private_stream_coded = 1;
275 int buf_index, i, private_stream_coded;
283 private_stream_coded = 0;
286 if (stream->
id < 0xc0) {
287 if (private_stream_coded)
289 private_stream_coded = 1;
299 int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j;
348 stream->
id = ac3_id++;
350 stream->
id = dts_id++;
352 stream->
id = lpcm_id++;
353 for(j = 0; j < 4; j++) {
366 stream->
id = mpa_id++;
375 stream->
id = mpv_id++;
389 stream->
id = mps_id++;
415 bitrate += codec_rate;
418 audio_bitrate += codec_rate;
420 video_bitrate += codec_rate;
428 bitrate += bitrate / 20;
430 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;
551 for(i=0;i<packet_bytes;i++)
563 pkt_desc= pkt_desc->
next;
571 int64_t pts, int64_t dts, int64_t scr,
int trailer_size)
576 int size, payload_size, startcode,
id, stuffing_size, i, header_len;
579 int zero_trail_bytes = 0;
580 int pad_packet_bytes = 0;
582 int general_pack = 0;
587 av_dlog(ctx,
"packet ID=%2x PTS=%0.3f\n",
id, pts / 90000.0);
608 int PES_bytes_to_fill = s->
packet_size - size - 10;
612 PES_bytes_to_fill -= 5 + 5;
614 PES_bytes_to_fill -= 5;
626 for (i = 0; i < 979; i++)
632 for (i = 0; i < 1017; i++)
635 memset(buffer, 0, 128);
663 zero_trail_bytes += 20;
675 pad_packet_bytes = packet_size - zero_trail_bytes;
678 packet_size -= pad_packet_bytes + zero_trail_bytes;
680 if (packet_size > 0) {
704 payload_size = packet_size - header_len;
714 startcode = 0x100 +
id;
725 timestamp_len += s->
is_mpeg2 ? 5 : 4;
727 header_len -= timestamp_len;
729 pad_packet_bytes += timestamp_len;
730 packet_size -= timestamp_len;
732 payload_size += timestamp_len;
734 stuffing_size += timestamp_len;
735 if(payload_size > trailer_size)
736 stuffing_size += payload_size - trailer_size;
739 if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) {
740 packet_size += pad_packet_bytes;
741 payload_size += pad_packet_bytes;
742 if (stuffing_size < 0) {
743 stuffing_size = pad_packet_bytes;
745 stuffing_size += pad_packet_bytes;
747 pad_packet_bytes = 0;
750 if (stuffing_size < 0)
755 stuffing_size += payload_size % stream->
lpcm_align;
758 if (stuffing_size > 16) {
759 pad_packet_bytes += stuffing_size;
760 packet_size -= stuffing_size;
761 payload_size -= stuffing_size;
765 nb_frames=
get_nb_frames(ctx, stream, payload_size - stuffing_size);
772 for(i=0;i<stuffing_size;i++)
794 avio_w8(ctx->
pb, header_len - 3 + stuffing_size);
796 if (pes_flags & 0x80)
798 if (pes_flags & 0x40)
801 if (pes_flags & 0x01) {
829 for(i=0;i<stuffing_size;i++)
842 }
else if (
id >= 0x40) {
858 if (pad_packet_bytes > 0)
861 for(i=0;i<zero_trail_bytes;i++)
874 return payload_size - stuffing_size;
912 && scr > pkt_desc->
dts){
916 "buffer underflow st=%d bufi=%d size=%d\n",
934 int i, avail_space=0, es_size, trailer_size;
936 int best_score= INT_MIN;
937 int ignore_constraints=0;
960 if(space < s->packet_size && !ignore_constraints)
963 if(next_pkt && next_pkt->
dts - scr > max_delay)
968 if(rel_space > best_score){
969 best_score= rel_space;
976 int64_t best_dts= INT64_MAX;
982 if(pkt_desc && pkt_desc->
dts < best_dts)
983 best_dts= pkt_desc->
dts;
986 av_dlog(ctx,
"bumping scr, scr:%f, dts:%f\n",
987 scr / 90000.0, best_dts / 90000.0);
988 if(best_dts == INT64_MAX)
991 if(scr >= best_dts+1 && !ignore_constraints){
993 ignore_constraints= 1;
995 scr=
FFMAX(best_dts+1, scr);
1001 assert(best_i >= 0);
1008 assert(avail_space >= s->
packet_size || ignore_constraints);
1015 timestamp_packet= timestamp_packet->
next;
1018 if(timestamp_packet){
1019 av_dlog(ctx,
"dts:%f pts:%f scr:%f stream:%d\n",
1020 timestamp_packet->
dts / 90000.0,
1021 timestamp_packet->
pts / 90000.0,
1022 scr / 90000.0, best_i);
1023 es_size=
flush_packet(ctx, best_i, timestamp_packet->
pts, timestamp_packet->
dts, scr, trailer_size);
1090 av_dlog(ctx,
"dts:%f pts:%f flags:%d stream:%d nopts:%d\n",
1091 dts / 90000.0, pts / 90000.0, pkt->
flags,
1154 #define OFFSET(x) offsetof(MpegMuxContext, x)
1155 #define E AV_OPT_FLAG_ENCODING_PARAM
1158 {
"preload",
"Initial demux-decode delay in microseconds.",
OFFSET(preload),
AV_OPT_TYPE_INT, {.i64 = 500000}, 0, INT_MAX,
E},
1162 #define MPEGENC_CLASS(flavor)\
1163 static const AVClass flavor ## _class = {\
1164 .class_name = #flavor " muxer",\
1165 .item_name = av_default_item_name,\
1166 .version = LIBAVUTIL_VERSION_INT,\
1170 #if CONFIG_MPEG1SYSTEM_MUXER
1175 .mime_type =
"video/mpeg",
1176 .extensions =
"mpg,mpeg",
1183 .priv_class = &mpeg_class,
1186 #if CONFIG_MPEG1VCD_MUXER
1191 .mime_type =
"video/mpeg",
1198 .priv_class = &vcd_class,
1201 #if CONFIG_MPEG2VOB_MUXER
1206 .mime_type =
"video/mpeg",
1207 .extensions =
"vob",
1214 .priv_class = &vob_class,
1219 #if CONFIG_MPEG2SVCD_MUXER
1224 .mime_type =
"video/mpeg",
1225 .extensions =
"vob",
1232 .priv_class = &svcd_class,
1237 #if CONFIG_MPEG2DVD_MUXER
1242 .mime_type =
"video/mpeg",
1243 .extensions =
"dvd",
1250 .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)
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
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)
static const int lpcm_freq_tab[4]
AVCodecContext * codec
Codec context associated with this stream.
static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len)
int64_t vcd_padding_bytes_written
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", 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)
double vcd_padding_bitrate
#define CONFIG_MPEG2VOB_MUXER
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
static int output_packet(AVFormatContext *ctx, int flush)
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 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
PacketDesc * predecode_packet
void av_fifo_free(AVFifoBuffer *f)
Free an AVFifoBuffer.
const OptionDef options[]
static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
#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(). ...
void * priv_data
Format private data.
#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 rc_max_rate
maximum bitrate
int flags
A combination of AV_PKT_FLAG values.
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 rc_buffer_size
decoder bitstream buffer size
unsigned int nb_streams
A list of all streams in the file.
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int size)
Resize an AVFifoBuffer.
static void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
int bit_rate
the average bitrate
int64_t av_rescale(int64_t a, int64_t b, int64_t c) av_const
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 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
static void flush(AVCodecContext *avctx)
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
AVIOContext * pb
I/O context.
Describe the class of an AVClass context structure.
#define CONFIG_MPEG2DVD_MUXER
AVFifoBuffer * av_fifo_alloc(unsigned int size)
Initialize an AVFifoBuffer.
AVOutputFormat ff_mpeg2dvd_muxer
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.
PacketDesc * premux_packet
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.
struct AVOutputFormat * oformat
int channels
number of audio channels
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...
static int put_system_header(AVFormatContext *ctx, uint8_t *buf, int only_for_stream_id)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
This structure stores compressed data.
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
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.
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
AVOutputFormat ff_mpeg2vob_muxer