/*
* MPEG1/2 muxer
- * Copyright (c) 2000, 2001, 2002 Fabrice Bellard.
+ * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
*
- * This file is part of FFmpeg.
+ * This file is part of Libav.
*
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
- * FFmpeg is distributed in the hope that it will be useful,
+ * Libav is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
+ * License along with Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#include "libavutil/attributes.h"
#include "libavutil/fifo.h"
-#include "libavcodec/bitstream.h"
+#include "libavutil/log.h"
+#include "libavutil/mathematics.h"
+#include "libavutil/opt.h"
+#include "libavcodec/put_bits.h"
#include "avformat.h"
+#include "internal.h"
#include "mpeg.h"
#define MAX_PAYLOAD_SIZE 4096
-//#define DEBUG_SEEK
#undef NDEBUG
#include <assert.h>
} PacketDesc;
typedef struct {
- AVFifoBuffer fifo;
+ AVFifoBuffer *fifo;
uint8_t id;
int max_buffer_size; /* in bytes */
int buffer_index;
} StreamInfo;
typedef struct {
+ const AVClass *class;
int packet_size; /* required packet size */
int packet_number;
int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */
double vcd_padding_bitrate; //FIXME floats
int64_t vcd_padding_bytes_written;
+ int preload;
} MpegMuxContext;
-extern AVOutputFormat mpeg1vcd_muxer;
-extern AVOutputFormat mpeg2dvd_muxer;
-extern AVOutputFormat mpeg2svcd_muxer;
-extern AVOutputFormat mpeg2vob_muxer;
+extern AVOutputFormat ff_mpeg1vcd_muxer;
+extern AVOutputFormat ff_mpeg2dvd_muxer;
+extern AVOutputFormat ff_mpeg2svcd_muxer;
+extern AVOutputFormat ff_mpeg2vob_muxer;
static int put_pack_header(AVFormatContext *ctx,
uint8_t *buf, int64_t timestamp)
init_put_bits(&pb, buf, 128);
- put_bits(&pb, 32, PACK_START_CODE);
+ put_bits32(&pb, PACK_START_CODE);
if (s->is_mpeg2) {
put_bits(&pb, 2, 0x1);
} else {
put_bits(&pb, 3, 0); /* stuffing length */
}
flush_put_bits(&pb);
- return pbBufPtr(&pb) - pb.buf;
+ return put_bits_ptr(&pb) - pb.buf;
}
static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id)
init_put_bits(&pb, buf, 128);
- put_bits(&pb, 32, SYSTEM_HEADER_START_CODE);
+ put_bits32(&pb, SYSTEM_HEADER_START_CODE);
put_bits(&pb, 16, 0);
put_bits(&pb, 1, 1);
put_bits(&pb, 1, 1); /* marker */
- if (s->is_vcd && only_for_stream_id==AUDIO_ID) {
+ if (s->is_vcd && (only_for_stream_id & 0xe0) == AUDIO_ID) {
/* This header applies only to the audio stream (see VCD standard p. IV-7)*/
put_bits(&pb, 5, 0);
} else
}
flush_put_bits(&pb);
- size = pbBufPtr(&pb) - pb.buf;
+ size = put_bits_ptr(&pb) - pb.buf;
/* patch packet size */
buf[4] = (size - 6) >> 8;
buf[5] = (size - 6) & 0xff;
return buf_index;
}
-static int mpeg_mux_init(AVFormatContext *ctx)
+static av_cold int mpeg_mux_init(AVFormatContext *ctx)
{
MpegMuxContext *s = ctx->priv_data;
int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j;
int video_bitrate;
s->packet_number = 0;
- s->is_vcd = (ENABLE_MPEG1VCD_MUXER && ctx->oformat == &mpeg1vcd_muxer);
- s->is_svcd = (ENABLE_MPEG2SVCD_MUXER && ctx->oformat == &mpeg2svcd_muxer);
- s->is_mpeg2 = ((ENABLE_MPEG2VOB_MUXER && ctx->oformat == &mpeg2vob_muxer) ||
- (ENABLE_MPEG2DVD_MUXER && ctx->oformat == &mpeg2dvd_muxer) ||
- (ENABLE_MPEG2SVCD_MUXER && ctx->oformat == &mpeg2svcd_muxer));
- s->is_dvd = (ENABLE_MPEG2DVD_MUXER && ctx->oformat == &mpeg2dvd_muxer);
-
- if(ctx->packet_size)
+ s->is_vcd = (CONFIG_MPEG1VCD_MUXER && ctx->oformat == &ff_mpeg1vcd_muxer);
+ s->is_svcd = (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer);
+ s->is_mpeg2 = ((CONFIG_MPEG2VOB_MUXER && ctx->oformat == &ff_mpeg2vob_muxer) ||
+ (CONFIG_MPEG2DVD_MUXER && ctx->oformat == &ff_mpeg2dvd_muxer) ||
+ (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer));
+ s->is_dvd = (CONFIG_MPEG2DVD_MUXER && ctx->oformat == &ff_mpeg2dvd_muxer);
+
+ if(ctx->packet_size) {
+ if (ctx->packet_size < 20 || ctx->packet_size > (1 << 23) + 10) {
+ av_log(ctx, AV_LOG_ERROR, "Invalid packet size %d\n",
+ ctx->packet_size);
+ goto fail;
+ }
s->packet_size = ctx->packet_size;
- else
+ } else
s->packet_size = 2048;
+ if (ctx->max_delay < 0) /* Not set by the caller */
+ ctx->max_delay = 0;
s->vcd_padding_bytes_written = 0;
s->vcd_padding_bitrate=0;
goto fail;
st->priv_data = stream;
- av_set_pts_info(st, 64, 1, 90000);
+ avpriv_set_pts_info(st, 64, 1, 90000);
switch(st->codec->codec_type) {
- case CODEC_TYPE_AUDIO:
- if (st->codec->codec_id == CODEC_ID_AC3) {
+ case AVMEDIA_TYPE_AUDIO:
+ if (st->codec->codec_id == AV_CODEC_ID_AC3) {
stream->id = ac3_id++;
- } else if (st->codec->codec_id == CODEC_ID_DTS) {
+ } else if (st->codec->codec_id == AV_CODEC_ID_DTS) {
stream->id = dts_id++;
- } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
+ } else if (st->codec->codec_id == AV_CODEC_ID_PCM_S16BE) {
stream->id = lpcm_id++;
for(j = 0; j < 4; j++) {
if (lpcm_freq_tab[j] == st->codec->sample_rate)
stream->max_buffer_size = 4 * 1024;
s->audio_bound++;
break;
- case CODEC_TYPE_VIDEO:
+ case AVMEDIA_TYPE_VIDEO:
stream->id = mpv_id++;
if (st->codec->rc_buffer_size)
stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8;
- else
+ else {
+ av_log(ctx, AV_LOG_WARNING, "VBV buffer size not set, muxing may fail\n");
stream->max_buffer_size = 230*1024; //FIXME this is probably too small as default
-#if 0
- /* see VCD standard, p. IV-7*/
- stream->max_buffer_size = 46 * 1024;
- else
- /* This value HAS to be used for SVCD (see SVCD standard, p. 26 V.2.3.2).
- Right now it is also used for everything else.*/
- stream->max_buffer_size = 230 * 1024;
-#endif
+ }
s->video_bound++;
break;
- case CODEC_TYPE_SUBTITLE:
+ case AVMEDIA_TYPE_SUBTITLE:
stream->id = mps_id++;
stream->max_buffer_size = 16 * 1024;
break;
default:
return -1;
}
- av_fifo_init(&stream->fifo, 16);
+ stream->fifo= av_fifo_alloc(16);
+ if (!stream->fifo)
+ goto fail;
}
bitrate = 0;
audio_bitrate = 0;
bitrate += codec_rate;
- if (stream->id==AUDIO_ID)
+ if ((stream->id & 0xe0) == AUDIO_ID)
audio_bitrate += codec_rate;
else if (stream->id==VIDEO_ID)
video_bitrate += codec_rate;
}
- if(ctx->mux_rate){
- s->mux_rate= (ctx->mux_rate + (8 * 50) - 1) / (8 * 50);
- } else {
+ if (!s->mux_rate) {
/* we increase slightly the bitrate to take into account the
headers. XXX: compute it exactly */
- bitrate += bitrate*5/100;
+ bitrate += bitrate / 20;
bitrate += 10000;
s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
}
return AVERROR(ENOMEM);
}
-static inline void put_timestamp(ByteIOContext *pb, int id, int64_t timestamp)
+static inline void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
{
- put_byte(pb,
+ avio_w8(pb,
(id << 4) |
(((timestamp >> 30) & 0x07) << 1) |
1);
- put_be16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
- put_be16(pb, (uint16_t)((((timestamp ) & 0x7fff) << 1) | 1));
+ avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
+ avio_wb16(pb, (uint16_t)((((timestamp ) & 0x7fff) << 1) | 1));
}
}
-#if 0 /* unused, remove? */
-/* return the exact available payload size for the next packet for
- stream 'stream_index'. 'pts' and 'dts' are only used to know if
- timestamps are needed in the packet header. */
-static int get_packet_payload_size(AVFormatContext *ctx, int stream_index,
- int64_t pts, int64_t dts)
-{
- MpegMuxContext *s = ctx->priv_data;
- int buf_index;
- StreamInfo *stream;
-
- stream = ctx->streams[stream_index]->priv_data;
-
- buf_index = 0;
- if (((s->packet_number % s->pack_header_freq) == 0)) {
- /* pack header size */
- if (s->is_mpeg2)
- buf_index += 14;
- else
- buf_index += 12;
-
- if (s->is_vcd) {
- /* there is exactly one system header for each stream in a VCD MPEG,
- One in the very first video packet and one in the very first
- audio packet (see VCD standard p. IV-7 and IV-8).*/
-
- if (stream->packet_number==0)
- /* The system headers refer only to the stream they occur in,
- so they have a constant size.*/
- buf_index += 15;
-
- } else {
- if ((s->packet_number % s->system_header_freq) == 0)
- buf_index += s->system_header_size;
- }
- }
-
- if ((s->is_vcd && stream->packet_number==0)
- || (s->is_svcd && s->packet_number==0))
- /* the first pack of each stream contains only the pack header,
- the system header and some padding (see VCD standard p. IV-6)
- Add the padding size, so that the actual payload becomes 0.*/
- buf_index += s->packet_size - buf_index;
- else {
- /* packet header size */
- buf_index += 6;
- if (s->is_mpeg2) {
- buf_index += 3;
- if (stream->packet_number==0)
- buf_index += 3; /* PES extension */
- buf_index += 1; /* obligatory stuffing byte */
- }
- if (pts != AV_NOPTS_VALUE) {
- if (dts != pts)
- buf_index += 5 + 5;
- else
- buf_index += 5;
-
- } else {
- if (!s->is_mpeg2)
- buf_index++;
- }
-
- if (stream->id < 0xc0) {
- /* AC-3/LPCM private data header */
- buf_index += 4;
- if (stream->id >= 0xa0) {
- int n;
- buf_index += 3;
- /* NOTE: we round the payload size to an integer number of
- LPCM samples */
- n = (s->packet_size - buf_index) % stream->lpcm_align;
- if (n)
- buf_index += (stream->lpcm_align - n);
- }
- }
-
- if (s->is_vcd && stream->id == AUDIO_ID)
- /* The VCD standard demands that 20 zero bytes follow
- each audio packet (see standard p. IV-8).*/
- buf_index+=20;
- }
- return s->packet_size - buf_index;
-}
-#endif
-
/* Write an MPEG padding packet header. */
-static void put_padding_packet(AVFormatContext *ctx, ByteIOContext *pb,int packet_bytes)
+static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb,int packet_bytes)
{
MpegMuxContext *s = ctx->priv_data;
int i;
- put_be32(pb, PADDING_STREAM);
- put_be16(pb, packet_bytes - 6);
+ avio_wb32(pb, PADDING_STREAM);
+ avio_wb16(pb, packet_bytes - 6);
if (!s->is_mpeg2) {
- put_byte(pb, 0x0f);
+ avio_w8(pb, 0x0f);
packet_bytes -= 7;
} else
packet_bytes -= 6;
for(i=0;i<packet_bytes;i++)
- put_byte(pb, 0xff);
+ avio_w8(pb, 0xff);
}
static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len){
id = stream->id;
-#if 0
- printf("packet ID=%2x PTS=%0.3f\n",
- id, pts / 90000.0);
-#endif
+ av_dlog(ctx, "packet ID=%2x PTS=%0.3f\n", id, pts / 90000.0);
buf_ptr = buffer;
size = put_system_header(ctx, buf_ptr, 0);
buf_ptr += size;
size = buf_ptr - buffer;
- put_buffer(ctx->pb, buffer, size);
+ avio_write(ctx->pb, buffer, size);
- put_be32(ctx->pb, PRIVATE_STREAM_2);
- put_be16(ctx->pb, 0x03d4); // length
- put_byte(ctx->pb, 0x00); // substream ID, 00=PCI
+ avio_wb32(ctx->pb, PRIVATE_STREAM_2);
+ avio_wb16(ctx->pb, 0x03d4); // length
+ avio_w8(ctx->pb, 0x00); // substream ID, 00=PCI
for (i = 0; i < 979; i++)
- put_byte(ctx->pb, 0x00);
+ avio_w8(ctx->pb, 0x00);
- put_be32(ctx->pb, PRIVATE_STREAM_2);
- put_be16(ctx->pb, 0x03fa); // length
- put_byte(ctx->pb, 0x01); // substream ID, 01=DSI
+ avio_wb32(ctx->pb, PRIVATE_STREAM_2);
+ avio_wb16(ctx->pb, 0x03fa); // length
+ avio_w8(ctx->pb, 0x01); // substream ID, 01=DSI
for (i = 0; i < 1017; i++)
- put_byte(ctx->pb, 0x00);
+ avio_w8(ctx->pb, 0x00);
memset(buffer, 0, 128);
buf_ptr = buffer;
}
}
size = buf_ptr - buffer;
- put_buffer(ctx->pb, buffer, size);
+ avio_write(ctx->pb, buffer, size);
packet_size = s->packet_size - size;
- if (s->is_vcd && id == AUDIO_ID)
+ if (s->is_vcd && (id & 0xe0) == AUDIO_ID)
/* The VCD standard demands that 20 zero bytes follow
each audio pack (see standard p. IV-8).*/
zero_trail_bytes += 20;
startcode = 0x100 + id;
}
- stuffing_size = payload_size - av_fifo_size(&stream->fifo);
+ stuffing_size = payload_size - av_fifo_size(stream->fifo);
// first byte does not fit -> reset pts/dts + stuffing
if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){
if (stuffing_size < 0)
stuffing_size = 0;
+
+ if (startcode == PRIVATE_STREAM_1 && id >= 0xa0) {
+ if (payload_size < av_fifo_size(stream->fifo))
+ stuffing_size += payload_size % stream->lpcm_align;
+ }
+
if (stuffing_size > 16) { /*<=16 for MPEG-1, <=32 for MPEG-2*/
pad_packet_bytes += stuffing_size;
packet_size -= stuffing_size;
nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size);
- put_be32(ctx->pb, startcode);
+ avio_wb32(ctx->pb, startcode);
- put_be16(ctx->pb, packet_size);
+ avio_wb16(ctx->pb, packet_size);
if (!s->is_mpeg2)
for(i=0;i<stuffing_size;i++)
- put_byte(ctx->pb, 0xff);
+ avio_w8(ctx->pb, 0xff);
if (s->is_mpeg2) {
- put_byte(ctx->pb, 0x80); /* mpeg2 id */
+ avio_w8(ctx->pb, 0x80); /* mpeg2 id */
pes_flags=0;
if (stream->packet_number == 0)
pes_flags |= 0x01;
- put_byte(ctx->pb, pes_flags); /* flags */
- put_byte(ctx->pb, header_len - 3 + stuffing_size);
+ avio_w8(ctx->pb, pes_flags); /* flags */
+ avio_w8(ctx->pb, header_len - 3 + stuffing_size);
if (pes_flags & 0x80) /*write pts*/
put_timestamp(ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
put_timestamp(ctx->pb, 0x01, dts);
if (pes_flags & 0x01) { /*write pes extension*/
- put_byte(ctx->pb, 0x10); /* flags */
+ avio_w8(ctx->pb, 0x10); /* flags */
/* P-STD buffer info */
- if (id == AUDIO_ID)
- put_be16(ctx->pb, 0x4000 | stream->max_buffer_size/ 128);
+ if ((id & 0xe0) == AUDIO_ID)
+ avio_wb16(ctx->pb, 0x4000 | stream->max_buffer_size/ 128);
else
- put_be16(ctx->pb, 0x6000 | stream->max_buffer_size/1024);
+ avio_wb16(ctx->pb, 0x6000 | stream->max_buffer_size/1024);
}
} else {
put_timestamp(ctx->pb, 0x02, pts);
}
} else {
- put_byte(ctx->pb, 0x0f);
+ avio_w8(ctx->pb, 0x0f);
}
}
if (s->is_mpeg2) {
/* special stuffing byte that is always written
to prevent accidental generation of start codes. */
- put_byte(ctx->pb, 0xff);
+ avio_w8(ctx->pb, 0xff);
for(i=0;i<stuffing_size;i++)
- put_byte(ctx->pb, 0xff);
+ avio_w8(ctx->pb, 0xff);
}
if (startcode == PRIVATE_STREAM_1) {
- put_byte(ctx->pb, id);
+ avio_w8(ctx->pb, id);
if (id >= 0xa0) {
/* LPCM (XXX: check nb_frames) */
- put_byte(ctx->pb, 7);
- put_be16(ctx->pb, 4); /* skip 3 header bytes */
- put_byte(ctx->pb, stream->lpcm_header[0]);
- put_byte(ctx->pb, stream->lpcm_header[1]);
- put_byte(ctx->pb, stream->lpcm_header[2]);
+ avio_w8(ctx->pb, 7);
+ avio_wb16(ctx->pb, 4); /* skip 3 header bytes */
+ avio_w8(ctx->pb, stream->lpcm_header[0]);
+ avio_w8(ctx->pb, stream->lpcm_header[1]);
+ avio_w8(ctx->pb, stream->lpcm_header[2]);
} else if (id >= 0x40) {
/* AC-3 */
- put_byte(ctx->pb, nb_frames);
- put_be16(ctx->pb, trailer_size+1);
+ avio_w8(ctx->pb, nb_frames);
+ avio_wb16(ctx->pb, trailer_size+1);
}
}
/* output data */
- assert(payload_size - stuffing_size <= av_fifo_size(&stream->fifo));
- av_fifo_generic_read(&stream->fifo, payload_size - stuffing_size, &put_buffer, ctx->pb);
+ assert(payload_size - stuffing_size <= av_fifo_size(stream->fifo));
+ av_fifo_generic_read(stream->fifo, ctx->pb, payload_size - stuffing_size, &avio_write);
stream->bytes_to_iframe -= payload_size - stuffing_size;
}else{
payload_size=
put_padding_packet(ctx,ctx->pb, pad_packet_bytes);
for(i=0;i<zero_trail_bytes;i++)
- put_byte(ctx->pb, 0x00);
+ avio_w8(ctx->pb, 0x00);
- put_flush_packet(ctx->pb);
+ avio_flush(ctx->pb);
s->packet_number++;
int i;
for(i=0;i<s->packet_size;i++)
- put_byte(ctx->pb, 0);
+ avio_w8(ctx->pb, 0);
s->vcd_padding_bytes_written += s->packet_size;
- put_flush_packet(ctx->pb);
+ avio_flush(ctx->pb);
/* increasing the packet number is correct. The SCR of the following packs
is calculated from the packet_number and it has to include the padding
s->packet_number++;
}
-#if 0 /* unused, remove? */
-static int64_t get_vcd_scr(AVFormatContext *ctx,int stream_index,int64_t pts)
-{
- MpegMuxContext *s = ctx->priv_data;
- int64_t scr;
-
- /* Since the data delivery rate is constant, SCR is computed
- using the formula C + i * 1200 where C is the start constant
- and i is the pack index.
- It is recommended that SCR 0 is at the beginning of the VCD front
- margin (a sequence of empty Form 2 sectors on the CD).
- It is recommended that the front margin is 30 sectors long, so
- we use C = 30*1200 = 36000
- (Note that even if the front margin is not 30 sectors the file
- will still be correct according to the standard. It just won't have
- the "recommended" value).*/
- scr = 36000 + s->packet_number * 1200;
-
- return scr;
-}
-#endif
-
static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){
// MpegMuxContext *s = ctx->priv_data;
int i;
for(i=0; i<ctx->nb_streams; i++){
AVStream *st = ctx->streams[i];
StreamInfo *stream = st->priv_data;
- const int avail_data= av_fifo_size(&stream->fifo);
+ const int avail_data= av_fifo_size(stream->fifo);
const int space= stream->max_buffer_size - stream->buffer_index;
int rel_space= 1024*space / stream->max_buffer_size;
PacketDesc *next_pkt= stream->premux_packet;
/* for subtitle, a single PES packet must be generated,
so we flush after every single subtitle packet */
if(s->packet_size > avail_data && !flush
- && st->codec->codec_type != CODEC_TYPE_SUBTITLE)
+ && st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE)
return 0;
if(avail_data==0)
continue;
best_dts= pkt_desc->dts;
}
-#if 0
- av_log(ctx, AV_LOG_DEBUG, "bumping scr, scr:%f, dts:%f\n",
- scr/90000.0, best_dts/90000.0);
-#endif
+ av_dlog(ctx, "bumping scr, scr:%f, dts:%f\n",
+ scr / 90000.0, best_dts / 90000.0);
if(best_dts == INT64_MAX)
return 0;
st = ctx->streams[best_i];
stream = st->priv_data;
- assert(av_fifo_size(&stream->fifo) > 0);
+ assert(av_fifo_size(stream->fifo) > 0);
assert(avail_space >= s->packet_size || ignore_constraints);
}
if(timestamp_packet){
-//av_log(ctx, AV_LOG_DEBUG, "dts:%f pts:%f scr:%f stream:%d\n", timestamp_packet->dts/90000.0, timestamp_packet->pts/90000.0, scr/90000.0, best_i);
+ av_dlog(ctx, "dts:%f pts:%f scr:%f stream:%d\n",
+ timestamp_packet->dts / 90000.0,
+ timestamp_packet->pts / 90000.0,
+ scr / 90000.0, best_i);
es_size= flush_packet(ctx, best_i, timestamp_packet->pts, timestamp_packet->dts, scr, trailer_size);
}else{
- assert(av_fifo_size(&stream->fifo) == trailer_size);
+ assert(av_fifo_size(stream->fifo) == trailer_size);
es_size= flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr, trailer_size);
}
StreamInfo *stream = st->priv_data;
int64_t pts, dts;
PacketDesc *pkt_desc;
- const int preload= av_rescale(ctx->preload, 90000, AV_TIME_BASE);
- const int is_iframe = st->codec->codec_type == CODEC_TYPE_VIDEO && (pkt->flags & PKT_FLAG_KEY);
+ int preload;
+ const int is_iframe = st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (pkt->flags & AV_PKT_FLAG_KEY);
+
+ preload = av_rescale(s->preload, 90000, AV_TIME_BASE);
pts= pkt->pts;
dts= pkt->dts;
- if(pts != AV_NOPTS_VALUE) pts += preload;
- if(dts != AV_NOPTS_VALUE) dts += preload;
+ if(pts != AV_NOPTS_VALUE) pts += 2*preload;
+ if(dts != AV_NOPTS_VALUE){
+ if(!s->last_scr)
+ s->last_scr= dts + preload;
+ dts += 2*preload;
+ }
-//av_log(ctx, AV_LOG_DEBUG, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n", dts/90000.0, pts/90000.0, pkt->flags, pkt->stream_index, pts != AV_NOPTS_VALUE);
+ av_dlog(ctx, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n",
+ dts / 90000.0, pts / 90000.0, pkt->flags,
+ pkt->stream_index, pts != AV_NOPTS_VALUE);
if (!stream->premux_packet)
stream->next_packet = &stream->premux_packet;
*stream->next_packet=
stream->predecode_packet= pkt_desc;
stream->next_packet= &pkt_desc->next;
- if (av_fifo_realloc2(&stream->fifo, av_fifo_size(&stream->fifo) + size) < 0)
+ if (av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size) < 0)
return -1;
if (s->is_dvd){
if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) { // min VOBU length 0.4 seconds (mpucoder)
- stream->bytes_to_iframe = av_fifo_size(&stream->fifo);
+ stream->bytes_to_iframe = av_fifo_size(stream->fifo);
stream->align_iframe = 1;
stream->vobu_start_pts = pts;
}
}
- av_fifo_generic_write(&stream->fifo, buf, size, NULL);
+ av_fifo_generic_write(stream->fifo, buf, size, NULL);
for(;;){
int ret= output_packet(ctx, 0);
/* End header according to MPEG1 systems standard. We do not write
it as it is usually not needed by decoders and because it
complicates MPEG stream concatenation. */
- //put_be32(ctx->pb, ISO_11172_END_CODE);
- //put_flush_packet(ctx->pb);
+ //avio_wb32(ctx->pb, ISO_11172_END_CODE);
+ //avio_flush(ctx->pb);
for(i=0;i<ctx->nb_streams;i++) {
stream = ctx->streams[i]->priv_data;
- assert(av_fifo_size(&stream->fifo) == 0);
- av_fifo_free(&stream->fifo);
+ assert(av_fifo_size(stream->fifo) == 0);
+ av_fifo_free(stream->fifo);
}
return 0;
}
+#define OFFSET(x) offsetof(MpegMuxContext, x)
+#define E AV_OPT_FLAG_ENCODING_PARAM
+static const AVOption options[] = {
+ { "muxrate", NULL, OFFSET(mux_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E },
+ { "preload", "Initial demux-decode delay in microseconds.", OFFSET(preload), AV_OPT_TYPE_INT, {.i64 = 500000}, 0, INT_MAX, E},
+ { NULL },
+};
+
+#define MPEGENC_CLASS(flavor)\
+static const AVClass flavor ## _class = {\
+ .class_name = #flavor " muxer",\
+ .item_name = av_default_item_name,\
+ .version = LIBAVUTIL_VERSION_INT,\
+ .option = options,\
+};
+
#if CONFIG_MPEG1SYSTEM_MUXER
-AVOutputFormat mpeg1system_muxer = {
- "mpeg",
- NULL_IF_CONFIG_SMALL("MPEG-1 System format"),
- "video/mpeg",
- "mpg,mpeg",
- sizeof(MpegMuxContext),
- CODEC_ID_MP2,
- CODEC_ID_MPEG1VIDEO,
- mpeg_mux_init,
- mpeg_mux_write_packet,
- mpeg_mux_end,
+MPEGENC_CLASS(mpeg)
+AVOutputFormat ff_mpeg1system_muxer = {
+ .name = "mpeg",
+ .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream"),
+ .mime_type = "video/mpeg",
+ .extensions = "mpg,mpeg",
+ .priv_data_size = sizeof(MpegMuxContext),
+ .audio_codec = AV_CODEC_ID_MP2,
+ .video_codec = AV_CODEC_ID_MPEG1VIDEO,
+ .write_header = mpeg_mux_init,
+ .write_packet = mpeg_mux_write_packet,
+ .write_trailer = mpeg_mux_end,
+ .priv_class = &mpeg_class,
};
#endif
#if CONFIG_MPEG1VCD_MUXER
-AVOutputFormat mpeg1vcd_muxer = {
- "vcd",
- NULL_IF_CONFIG_SMALL("MPEG-1 System format (VCD)"),
- "video/mpeg",
- NULL,
- sizeof(MpegMuxContext),
- CODEC_ID_MP2,
- CODEC_ID_MPEG1VIDEO,
- mpeg_mux_init,
- mpeg_mux_write_packet,
- mpeg_mux_end,
+MPEGENC_CLASS(vcd)
+AVOutputFormat ff_mpeg1vcd_muxer = {
+ .name = "vcd",
+ .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream (VCD)"),
+ .mime_type = "video/mpeg",
+ .priv_data_size = sizeof(MpegMuxContext),
+ .audio_codec = AV_CODEC_ID_MP2,
+ .video_codec = AV_CODEC_ID_MPEG1VIDEO,
+ .write_header = mpeg_mux_init,
+ .write_packet = mpeg_mux_write_packet,
+ .write_trailer = mpeg_mux_end,
+ .priv_class = &vcd_class,
};
#endif
#if CONFIG_MPEG2VOB_MUXER
-AVOutputFormat mpeg2vob_muxer = {
- "vob",
- NULL_IF_CONFIG_SMALL("MPEG-2 PS format (VOB)"),
- "video/mpeg",
- "vob",
- sizeof(MpegMuxContext),
- CODEC_ID_MP2,
- CODEC_ID_MPEG2VIDEO,
- mpeg_mux_init,
- mpeg_mux_write_packet,
- mpeg_mux_end,
+MPEGENC_CLASS(vob)
+AVOutputFormat ff_mpeg2vob_muxer = {
+ .name = "vob",
+ .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (VOB)"),
+ .mime_type = "video/mpeg",
+ .extensions = "vob",
+ .priv_data_size = sizeof(MpegMuxContext),
+ .audio_codec = AV_CODEC_ID_MP2,
+ .video_codec = AV_CODEC_ID_MPEG2VIDEO,
+ .write_header = mpeg_mux_init,
+ .write_packet = mpeg_mux_write_packet,
+ .write_trailer = mpeg_mux_end,
+ .priv_class = &vob_class,
};
#endif
/* Same as mpeg2vob_mux except that the pack size is 2324 */
#if CONFIG_MPEG2SVCD_MUXER
-AVOutputFormat mpeg2svcd_muxer = {
- "svcd",
- NULL_IF_CONFIG_SMALL("MPEG-2 PS format (VOB)"),
- "video/mpeg",
- "vob",
- sizeof(MpegMuxContext),
- CODEC_ID_MP2,
- CODEC_ID_MPEG2VIDEO,
- mpeg_mux_init,
- mpeg_mux_write_packet,
- mpeg_mux_end,
+MPEGENC_CLASS(svcd)
+AVOutputFormat ff_mpeg2svcd_muxer = {
+ .name = "svcd",
+ .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (SVCD)"),
+ .mime_type = "video/mpeg",
+ .extensions = "vob",
+ .priv_data_size = sizeof(MpegMuxContext),
+ .audio_codec = AV_CODEC_ID_MP2,
+ .video_codec = AV_CODEC_ID_MPEG2VIDEO,
+ .write_header = mpeg_mux_init,
+ .write_packet = mpeg_mux_write_packet,
+ .write_trailer = mpeg_mux_end,
+ .priv_class = &svcd_class,
};
#endif
/* Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
#if CONFIG_MPEG2DVD_MUXER
-AVOutputFormat mpeg2dvd_muxer = {
- "dvd",
- NULL_IF_CONFIG_SMALL("MPEG-2 PS format (DVD VOB)"),
- "video/mpeg",
- "dvd",
- sizeof(MpegMuxContext),
- CODEC_ID_MP2,
- CODEC_ID_MPEG2VIDEO,
- mpeg_mux_init,
- mpeg_mux_write_packet,
- mpeg_mux_end,
+MPEGENC_CLASS(dvd)
+AVOutputFormat ff_mpeg2dvd_muxer = {
+ .name = "dvd",
+ .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (DVD VOB)"),
+ .mime_type = "video/mpeg",
+ .extensions = "dvd",
+ .priv_data_size = sizeof(MpegMuxContext),
+ .audio_codec = AV_CODEC_ID_MP2,
+ .video_codec = AV_CODEC_ID_MPEG2VIDEO,
+ .write_header = mpeg_mux_init,
+ .write_packet = mpeg_mux_write_packet,
+ .write_trailer = mpeg_mux_end,
+ .priv_class = &dvd_class,
};
#endif