yading@11: /* yading@11: * MPEG1/2 muxer yading@11: * Copyright (c) 2000, 2001, 2002 Fabrice Bellard yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: #include "libavutil/fifo.h" yading@11: #include "libavutil/log.h" yading@11: #include "libavutil/mathematics.h" yading@11: #include "libavutil/opt.h" yading@11: #include "libavcodec/put_bits.h" yading@11: #include "avformat.h" yading@11: #include "internal.h" yading@11: #include "mpeg.h" yading@11: yading@11: #define MAX_PAYLOAD_SIZE 4096 yading@11: yading@11: #undef NDEBUG yading@11: #include yading@11: yading@11: typedef struct PacketDesc { yading@11: int64_t pts; yading@11: int64_t dts; yading@11: int size; yading@11: int unwritten_size; yading@11: int flags; yading@11: struct PacketDesc *next; yading@11: } PacketDesc; yading@11: yading@11: typedef struct { yading@11: AVFifoBuffer *fifo; yading@11: uint8_t id; yading@11: int max_buffer_size; /* in bytes */ yading@11: int buffer_index; yading@11: PacketDesc *predecode_packet; yading@11: PacketDesc *premux_packet; yading@11: PacketDesc **next_packet; yading@11: int packet_number; yading@11: uint8_t lpcm_header[3]; yading@11: int lpcm_align; yading@11: int bytes_to_iframe; yading@11: int align_iframe; yading@11: int64_t vobu_start_pts; yading@11: } StreamInfo; yading@11: yading@11: typedef struct { yading@11: const AVClass *class; yading@11: int packet_size; /* required packet size */ yading@11: int packet_number; yading@11: int pack_header_freq; /* frequency (in packets^-1) at which we send pack headers */ yading@11: int system_header_freq; yading@11: int system_header_size; yading@11: int user_mux_rate; /* bitrate in units of bits/s */ yading@11: int mux_rate; /* bitrate in units of 50 bytes/s */ yading@11: /* stream info */ yading@11: int audio_bound; yading@11: int video_bound; yading@11: int is_mpeg2; yading@11: int is_vcd; yading@11: int is_svcd; yading@11: int is_dvd; yading@11: int64_t last_scr; /* current system clock */ yading@11: yading@11: double vcd_padding_bitrate; //FIXME floats yading@11: int64_t vcd_padding_bytes_written; yading@11: yading@11: int preload; yading@11: } MpegMuxContext; yading@11: yading@11: extern AVOutputFormat ff_mpeg1vcd_muxer; yading@11: extern AVOutputFormat ff_mpeg2dvd_muxer; yading@11: extern AVOutputFormat ff_mpeg2svcd_muxer; yading@11: extern AVOutputFormat ff_mpeg2vob_muxer; yading@11: yading@11: static int put_pack_header(AVFormatContext *ctx, yading@11: uint8_t *buf, int64_t timestamp) yading@11: { yading@11: MpegMuxContext *s = ctx->priv_data; yading@11: PutBitContext pb; yading@11: yading@11: init_put_bits(&pb, buf, 128); yading@11: yading@11: put_bits32(&pb, PACK_START_CODE); yading@11: if (s->is_mpeg2) { yading@11: put_bits(&pb, 2, 0x1); yading@11: } else { yading@11: put_bits(&pb, 4, 0x2); yading@11: } yading@11: put_bits(&pb, 3, (uint32_t)((timestamp >> 30) & 0x07)); yading@11: put_bits(&pb, 1, 1); yading@11: put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff)); yading@11: put_bits(&pb, 1, 1); yading@11: put_bits(&pb, 15, (uint32_t)((timestamp ) & 0x7fff)); yading@11: put_bits(&pb, 1, 1); yading@11: if (s->is_mpeg2) { yading@11: /* clock extension */ yading@11: put_bits(&pb, 9, 0); yading@11: } yading@11: put_bits(&pb, 1, 1); yading@11: put_bits(&pb, 22, s->mux_rate); yading@11: put_bits(&pb, 1, 1); yading@11: if (s->is_mpeg2) { yading@11: put_bits(&pb, 1, 1); yading@11: put_bits(&pb, 5, 0x1f); /* reserved */ yading@11: put_bits(&pb, 3, 0); /* stuffing length */ yading@11: } yading@11: flush_put_bits(&pb); yading@11: return put_bits_ptr(&pb) - pb.buf; yading@11: } yading@11: yading@11: static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id) yading@11: { yading@11: MpegMuxContext *s = ctx->priv_data; yading@11: int size, i, private_stream_coded, id; yading@11: PutBitContext pb; yading@11: yading@11: init_put_bits(&pb, buf, 128); yading@11: yading@11: put_bits32(&pb, SYSTEM_HEADER_START_CODE); yading@11: put_bits(&pb, 16, 0); yading@11: put_bits(&pb, 1, 1); yading@11: yading@11: put_bits(&pb, 22, s->mux_rate); /* maximum bit rate of the multiplexed stream */ yading@11: put_bits(&pb, 1, 1); /* marker */ yading@11: if (s->is_vcd && only_for_stream_id==VIDEO_ID) { yading@11: /* This header applies only to the video stream (see VCD standard p. IV-7)*/ yading@11: put_bits(&pb, 6, 0); yading@11: } else yading@11: put_bits(&pb, 6, s->audio_bound); yading@11: yading@11: if (s->is_vcd) { yading@11: /* see VCD standard, p. IV-7*/ yading@11: put_bits(&pb, 1, 0); yading@11: put_bits(&pb, 1, 1); yading@11: } else { yading@11: put_bits(&pb, 1, 0); /* variable bitrate*/ yading@11: put_bits(&pb, 1, 0); /* non constrainted bit stream */ yading@11: } yading@11: yading@11: if (s->is_vcd || s->is_dvd) { yading@11: /* see VCD standard p IV-7 */ yading@11: put_bits(&pb, 1, 1); /* audio locked */ yading@11: put_bits(&pb, 1, 1); /* video locked */ yading@11: } else { yading@11: put_bits(&pb, 1, 0); /* audio locked */ yading@11: put_bits(&pb, 1, 0); /* video locked */ yading@11: } yading@11: yading@11: put_bits(&pb, 1, 1); /* marker */ yading@11: yading@11: if (s->is_vcd && (only_for_stream_id & 0xe0) == AUDIO_ID) { yading@11: /* This header applies only to the audio stream (see VCD standard p. IV-7)*/ yading@11: put_bits(&pb, 5, 0); yading@11: } else yading@11: put_bits(&pb, 5, s->video_bound); yading@11: yading@11: if (s->is_dvd) { yading@11: put_bits(&pb, 1, 0); /* packet_rate_restriction_flag */ yading@11: put_bits(&pb, 7, 0x7f); /* reserved byte */ yading@11: } else yading@11: put_bits(&pb, 8, 0xff); /* reserved byte */ yading@11: yading@11: /* DVD-Video Stream_bound entries yading@11: id (0xB9) video, maximum P-STD for stream 0xE0. (P-STD_buffer_bound_scale = 1) yading@11: id (0xB8) audio, maximum P-STD for any MPEG audio (0xC0 to 0xC7) streams. If there are none set to 4096 (32x128). (P-STD_buffer_bound_scale = 0) yading@11: id (0xBD) private stream 1 (audio other than MPEG and subpictures). (P-STD_buffer_bound_scale = 1) yading@11: id (0xBF) private stream 2, NAV packs, set to 2x1024. */ yading@11: if (s->is_dvd) { yading@11: yading@11: int P_STD_max_video = 0; yading@11: int P_STD_max_mpeg_audio = 0; yading@11: int P_STD_max_mpeg_PS1 = 0; yading@11: yading@11: for(i=0;inb_streams;i++) { yading@11: StreamInfo *stream = ctx->streams[i]->priv_data; yading@11: yading@11: id = stream->id; yading@11: if (id == 0xbd && stream->max_buffer_size > P_STD_max_mpeg_PS1) { yading@11: P_STD_max_mpeg_PS1 = stream->max_buffer_size; yading@11: } else if (id >= 0xc0 && id <= 0xc7 && stream->max_buffer_size > P_STD_max_mpeg_audio) { yading@11: P_STD_max_mpeg_audio = stream->max_buffer_size; yading@11: } else if (id == 0xe0 && stream->max_buffer_size > P_STD_max_video) { yading@11: P_STD_max_video = stream->max_buffer_size; yading@11: } yading@11: } yading@11: yading@11: /* video */ yading@11: put_bits(&pb, 8, 0xb9); /* stream ID */ yading@11: put_bits(&pb, 2, 3); yading@11: put_bits(&pb, 1, 1); yading@11: put_bits(&pb, 13, P_STD_max_video / 1024); yading@11: yading@11: /* audio */ yading@11: if (P_STD_max_mpeg_audio == 0) yading@11: P_STD_max_mpeg_audio = 4096; yading@11: put_bits(&pb, 8, 0xb8); /* stream ID */ yading@11: put_bits(&pb, 2, 3); yading@11: put_bits(&pb, 1, 0); yading@11: put_bits(&pb, 13, P_STD_max_mpeg_audio / 128); yading@11: yading@11: /* private stream 1 */ yading@11: put_bits(&pb, 8, 0xbd); /* stream ID */ yading@11: put_bits(&pb, 2, 3); yading@11: put_bits(&pb, 1, 0); yading@11: put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128); yading@11: yading@11: /* private stream 2 */ yading@11: put_bits(&pb, 8, 0xbf); /* stream ID */ yading@11: put_bits(&pb, 2, 3); yading@11: put_bits(&pb, 1, 1); yading@11: put_bits(&pb, 13, 2); yading@11: } yading@11: else { yading@11: /* audio stream info */ yading@11: private_stream_coded = 0; yading@11: for(i=0;inb_streams;i++) { yading@11: StreamInfo *stream = ctx->streams[i]->priv_data; yading@11: yading@11: yading@11: /* For VCDs, only include the stream info for the stream yading@11: that the pack which contains this system belongs to. yading@11: (see VCD standard p. IV-7) */ yading@11: if ( !s->is_vcd || stream->id==only_for_stream_id yading@11: || only_for_stream_id==0) { yading@11: yading@11: id = stream->id; yading@11: if (id < 0xc0) { yading@11: /* special case for private streams (AC-3 uses that) */ yading@11: if (private_stream_coded) yading@11: continue; yading@11: private_stream_coded = 1; yading@11: id = 0xbd; yading@11: } yading@11: put_bits(&pb, 8, id); /* stream ID */ yading@11: put_bits(&pb, 2, 3); yading@11: if (id < 0xe0) { yading@11: /* audio */ yading@11: put_bits(&pb, 1, 0); yading@11: put_bits(&pb, 13, stream->max_buffer_size / 128); yading@11: } else { yading@11: /* video */ yading@11: put_bits(&pb, 1, 1); yading@11: put_bits(&pb, 13, stream->max_buffer_size / 1024); yading@11: } yading@11: } yading@11: } yading@11: } yading@11: yading@11: flush_put_bits(&pb); yading@11: size = put_bits_ptr(&pb) - pb.buf; yading@11: /* patch packet size */ yading@11: buf[4] = (size - 6) >> 8; yading@11: buf[5] = (size - 6) & 0xff; yading@11: yading@11: return size; yading@11: } yading@11: yading@11: static int get_system_header_size(AVFormatContext *ctx) yading@11: { yading@11: int buf_index, i, private_stream_coded; yading@11: StreamInfo *stream; yading@11: MpegMuxContext *s = ctx->priv_data; yading@11: yading@11: if (s->is_dvd) yading@11: return 18; // DVD-Video system headers are 18 bytes fixed length. yading@11: yading@11: buf_index = 12; yading@11: private_stream_coded = 0; yading@11: for(i=0;inb_streams;i++) { yading@11: stream = ctx->streams[i]->priv_data; yading@11: if (stream->id < 0xc0) { yading@11: if (private_stream_coded) yading@11: continue; yading@11: private_stream_coded = 1; yading@11: } yading@11: buf_index += 3; yading@11: } yading@11: return buf_index; yading@11: } yading@11: yading@11: static int mpeg_mux_init(AVFormatContext *ctx) yading@11: { yading@11: MpegMuxContext *s = ctx->priv_data; yading@11: int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j; yading@11: AVStream *st; yading@11: StreamInfo *stream; yading@11: int audio_bitrate; yading@11: int video_bitrate; yading@11: yading@11: s->packet_number = 0; yading@11: s->is_vcd = (CONFIG_MPEG1VCD_MUXER && ctx->oformat == &ff_mpeg1vcd_muxer); yading@11: s->is_svcd = (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer); yading@11: s->is_mpeg2 = ((CONFIG_MPEG2VOB_MUXER && ctx->oformat == &ff_mpeg2vob_muxer) || yading@11: (CONFIG_MPEG2DVD_MUXER && ctx->oformat == &ff_mpeg2dvd_muxer) || yading@11: (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer)); yading@11: s->is_dvd = (CONFIG_MPEG2DVD_MUXER && ctx->oformat == &ff_mpeg2dvd_muxer); yading@11: yading@11: if(ctx->packet_size) { yading@11: if (ctx->packet_size < 20 || ctx->packet_size > (1 << 23) + 10) { yading@11: av_log(ctx, AV_LOG_ERROR, "Invalid packet size %d\n", yading@11: ctx->packet_size); yading@11: goto fail; yading@11: } yading@11: s->packet_size = ctx->packet_size; yading@11: } else yading@11: s->packet_size = 2048; yading@11: if (ctx->max_delay < 0) /* Not set by the caller */ yading@11: ctx->max_delay = 0; yading@11: yading@11: s->vcd_padding_bytes_written = 0; yading@11: s->vcd_padding_bitrate=0; yading@11: yading@11: s->audio_bound = 0; yading@11: s->video_bound = 0; yading@11: mpa_id = AUDIO_ID; yading@11: ac3_id = AC3_ID; yading@11: dts_id = DTS_ID; yading@11: mpv_id = VIDEO_ID; yading@11: mps_id = SUB_ID; yading@11: lpcm_id = LPCM_ID; yading@11: for(i=0;inb_streams;i++) { yading@11: st = ctx->streams[i]; yading@11: stream = av_mallocz(sizeof(StreamInfo)); yading@11: if (!stream) yading@11: goto fail; yading@11: st->priv_data = stream; yading@11: yading@11: avpriv_set_pts_info(st, 64, 1, 90000); yading@11: yading@11: switch(st->codec->codec_type) { yading@11: case AVMEDIA_TYPE_AUDIO: yading@11: if (st->codec->codec_id == AV_CODEC_ID_AC3) { yading@11: stream->id = ac3_id++; yading@11: } else if (st->codec->codec_id == AV_CODEC_ID_DTS) { yading@11: stream->id = dts_id++; yading@11: } else if (st->codec->codec_id == AV_CODEC_ID_PCM_S16BE) { yading@11: stream->id = lpcm_id++; yading@11: for(j = 0; j < 4; j++) { yading@11: if (lpcm_freq_tab[j] == st->codec->sample_rate) yading@11: break; yading@11: } yading@11: if (j == 4) yading@11: goto fail; yading@11: if (st->codec->channels > 8) yading@11: return -1; yading@11: stream->lpcm_header[0] = 0x0c; yading@11: stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4); yading@11: stream->lpcm_header[2] = 0x80; yading@11: stream->lpcm_align = st->codec->channels * 2; yading@11: } else { yading@11: stream->id = mpa_id++; yading@11: } yading@11: yading@11: /* This value HAS to be used for VCD (see VCD standard, p. IV-7). yading@11: Right now it is also used for everything else.*/ yading@11: stream->max_buffer_size = 4 * 1024; yading@11: s->audio_bound++; yading@11: break; yading@11: case AVMEDIA_TYPE_VIDEO: yading@11: stream->id = mpv_id++; yading@11: if (st->codec->rc_buffer_size) yading@11: stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8; yading@11: else { yading@11: av_log(ctx, AV_LOG_WARNING, "VBV buffer size not set, muxing may fail\n"); yading@11: stream->max_buffer_size = 230*1024; //FIXME this is probably too small as default yading@11: } yading@11: s->video_bound++; yading@11: break; yading@11: case AVMEDIA_TYPE_SUBTITLE: yading@11: stream->id = mps_id++; yading@11: stream->max_buffer_size = 16 * 1024; yading@11: break; yading@11: default: yading@11: return -1; yading@11: } yading@11: stream->fifo= av_fifo_alloc(16); yading@11: if (!stream->fifo) yading@11: goto fail; yading@11: } yading@11: bitrate = 0; yading@11: audio_bitrate = 0; yading@11: video_bitrate = 0; yading@11: for(i=0;inb_streams;i++) { yading@11: int codec_rate; yading@11: st = ctx->streams[i]; yading@11: stream = (StreamInfo*) st->priv_data; yading@11: yading@11: if(st->codec->rc_max_rate || stream->id==VIDEO_ID) yading@11: codec_rate= st->codec->rc_max_rate; yading@11: else yading@11: codec_rate= st->codec->bit_rate; yading@11: yading@11: if(!codec_rate) yading@11: codec_rate= (1<<21)*8*50/ctx->nb_streams; yading@11: yading@11: bitrate += codec_rate; yading@11: yading@11: if ((stream->id & 0xe0) == AUDIO_ID) yading@11: audio_bitrate += codec_rate; yading@11: else if (stream->id==VIDEO_ID) yading@11: video_bitrate += codec_rate; yading@11: } yading@11: yading@11: if (s->user_mux_rate) { yading@11: s->mux_rate = (s->user_mux_rate + (8 * 50) - 1) / (8 * 50); yading@11: } else { yading@11: /* we increase slightly the bitrate to take into account the yading@11: headers. XXX: compute it exactly */ yading@11: bitrate += bitrate / 20; yading@11: bitrate += 10000; yading@11: s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50); yading@11: } yading@11: yading@11: if (s->is_vcd) { yading@11: double overhead_rate; yading@11: yading@11: /* The VCD standard mandates that the mux_rate field is 3528 yading@11: (see standard p. IV-6). yading@11: The value is actually "wrong", i.e. if you calculate yading@11: it using the normal formula and the 75 sectors per second transfer yading@11: rate you get a different value because the real pack size is 2324, yading@11: not 2352. But the standard explicitly specifies that the mux_rate yading@11: field in the header must have this value.*/ yading@11: // s->mux_rate=2352 * 75 / 50; /* = 3528*/ yading@11: yading@11: /* The VCD standard states that the muxed stream must be yading@11: exactly 75 packs / second (the data rate of a single speed cdrom). yading@11: Since the video bitrate (probably 1150000 bits/sec) will be below yading@11: the theoretical maximum we have to add some padding packets yading@11: to make up for the lower data rate. yading@11: (cf. VCD standard p. IV-6 )*/ yading@11: yading@11: /* Add the header overhead to the data rate. yading@11: 2279 data bytes per audio pack, 2294 data bytes per video pack*/ yading@11: overhead_rate = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279); yading@11: overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294); yading@11: overhead_rate *= 8; yading@11: yading@11: /* Add padding so that the full bitrate is 2324*75 bytes/sec */ yading@11: s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate); yading@11: } yading@11: yading@11: if (s->is_vcd || s->is_mpeg2) yading@11: /* every packet */ yading@11: s->pack_header_freq = 1; yading@11: else yading@11: /* every 2 seconds */ yading@11: s->pack_header_freq = 2 * bitrate / s->packet_size / 8; yading@11: yading@11: /* the above seems to make pack_header_freq zero sometimes */ yading@11: if (s->pack_header_freq == 0) yading@11: s->pack_header_freq = 1; yading@11: yading@11: if (s->is_mpeg2) yading@11: /* every 200 packets. Need to look at the spec. */ yading@11: s->system_header_freq = s->pack_header_freq * 40; yading@11: else if (s->is_vcd) yading@11: /* the standard mandates that there are only two system headers yading@11: in the whole file: one in the first packet of each stream. yading@11: (see standard p. IV-7 and IV-8) */ yading@11: s->system_header_freq = 0x7fffffff; yading@11: else yading@11: s->system_header_freq = s->pack_header_freq * 5; yading@11: yading@11: for(i=0;inb_streams;i++) { yading@11: stream = ctx->streams[i]->priv_data; yading@11: stream->packet_number = 0; yading@11: } yading@11: s->system_header_size = get_system_header_size(ctx); yading@11: s->last_scr = AV_NOPTS_VALUE; yading@11: return 0; yading@11: fail: yading@11: for(i=0;inb_streams;i++) { yading@11: av_free(ctx->streams[i]->priv_data); yading@11: } yading@11: return AVERROR(ENOMEM); yading@11: } yading@11: yading@11: static inline void put_timestamp(AVIOContext *pb, int id, int64_t timestamp) yading@11: { yading@11: avio_w8(pb, yading@11: (id << 4) | yading@11: (((timestamp >> 30) & 0x07) << 1) | yading@11: 1); yading@11: avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1)); yading@11: avio_wb16(pb, (uint16_t)((((timestamp ) & 0x7fff) << 1) | 1)); yading@11: } yading@11: yading@11: yading@11: /* return the number of padding bytes that should be inserted into yading@11: the multiplexed stream.*/ yading@11: static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts) yading@11: { yading@11: MpegMuxContext *s = ctx->priv_data; yading@11: int pad_bytes = 0; yading@11: yading@11: if (s->vcd_padding_bitrate > 0 && pts!=AV_NOPTS_VALUE) yading@11: { yading@11: int64_t full_pad_bytes; yading@11: yading@11: full_pad_bytes = (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0); //FIXME this is wrong yading@11: pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written); yading@11: yading@11: if (pad_bytes<0) yading@11: /* might happen if we have already padded to a later timestamp. This yading@11: can occur if another stream has already advanced further.*/ yading@11: pad_bytes=0; yading@11: } yading@11: yading@11: return pad_bytes; yading@11: } yading@11: yading@11: yading@11: /* Write an MPEG padding packet header. */ yading@11: static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb,int packet_bytes) yading@11: { yading@11: MpegMuxContext *s = ctx->priv_data; yading@11: int i; yading@11: yading@11: avio_wb32(pb, PADDING_STREAM); yading@11: avio_wb16(pb, packet_bytes - 6); yading@11: if (!s->is_mpeg2) { yading@11: avio_w8(pb, 0x0f); yading@11: packet_bytes -= 7; yading@11: } else yading@11: packet_bytes -= 6; yading@11: yading@11: for(i=0;ipremux_packet; yading@11: yading@11: while(len>0){ yading@11: if(pkt_desc->size == pkt_desc->unwritten_size) yading@11: nb_frames++; yading@11: len -= pkt_desc->unwritten_size; yading@11: pkt_desc= pkt_desc->next; yading@11: } yading@11: yading@11: return nb_frames; yading@11: } yading@11: yading@11: /* flush the packet on stream stream_index */ yading@11: static int flush_packet(AVFormatContext *ctx, int stream_index, yading@11: int64_t pts, int64_t dts, int64_t scr, int trailer_size) yading@11: { yading@11: MpegMuxContext *s = ctx->priv_data; yading@11: StreamInfo *stream = ctx->streams[stream_index]->priv_data; yading@11: uint8_t *buf_ptr; yading@11: int size, payload_size, startcode, id, stuffing_size, i, header_len; yading@11: int packet_size; yading@11: uint8_t buffer[128]; yading@11: int zero_trail_bytes = 0; yading@11: int pad_packet_bytes = 0; yading@11: int pes_flags; yading@11: int general_pack = 0; /*"general" pack without data specific to one stream?*/ yading@11: int nb_frames; yading@11: yading@11: id = stream->id; yading@11: yading@11: av_dlog(ctx, "packet ID=%2x PTS=%0.3f\n", id, pts / 90000.0); yading@11: yading@11: buf_ptr = buffer; yading@11: yading@11: if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) { yading@11: /* output pack and systems header if needed */ yading@11: size = put_pack_header(ctx, buf_ptr, scr); yading@11: buf_ptr += size; yading@11: s->last_scr= scr; yading@11: yading@11: if (s->is_vcd) { yading@11: /* there is exactly one system header for each stream in a VCD MPEG, yading@11: One in the very first video packet and one in the very first yading@11: audio packet (see VCD standard p. IV-7 and IV-8).*/ yading@11: yading@11: if (stream->packet_number==0) { yading@11: size = put_system_header(ctx, buf_ptr, id); yading@11: buf_ptr += size; yading@11: } yading@11: } else if (s->is_dvd) { yading@11: if (stream->align_iframe || s->packet_number == 0){ yading@11: int PES_bytes_to_fill = s->packet_size - size - 10; yading@11: yading@11: if (pts != AV_NOPTS_VALUE) { yading@11: if (dts != pts) yading@11: PES_bytes_to_fill -= 5 + 5; yading@11: else yading@11: PES_bytes_to_fill -= 5; yading@11: } yading@11: yading@11: if (stream->bytes_to_iframe == 0 || s->packet_number == 0) { yading@11: size = put_system_header(ctx, buf_ptr, 0); yading@11: buf_ptr += size; yading@11: size = buf_ptr - buffer; yading@11: avio_write(ctx->pb, buffer, size); yading@11: yading@11: avio_wb32(ctx->pb, PRIVATE_STREAM_2); yading@11: avio_wb16(ctx->pb, 0x03d4); // length yading@11: avio_w8(ctx->pb, 0x00); // substream ID, 00=PCI yading@11: for (i = 0; i < 979; i++) yading@11: avio_w8(ctx->pb, 0x00); yading@11: yading@11: avio_wb32(ctx->pb, PRIVATE_STREAM_2); yading@11: avio_wb16(ctx->pb, 0x03fa); // length yading@11: avio_w8(ctx->pb, 0x01); // substream ID, 01=DSI yading@11: for (i = 0; i < 1017; i++) yading@11: avio_w8(ctx->pb, 0x00); yading@11: yading@11: memset(buffer, 0, 128); yading@11: buf_ptr = buffer; yading@11: s->packet_number++; yading@11: stream->align_iframe = 0; yading@11: scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet yading@11: size = put_pack_header(ctx, buf_ptr, scr); yading@11: s->last_scr= scr; yading@11: buf_ptr += size; yading@11: /* GOP Start */ yading@11: } else if (stream->bytes_to_iframe < PES_bytes_to_fill) { yading@11: pad_packet_bytes = PES_bytes_to_fill - stream->bytes_to_iframe; yading@11: } yading@11: } yading@11: } else { yading@11: if ((s->packet_number % s->system_header_freq) == 0) { yading@11: size = put_system_header(ctx, buf_ptr, 0); yading@11: buf_ptr += size; yading@11: } yading@11: } yading@11: } yading@11: size = buf_ptr - buffer; yading@11: avio_write(ctx->pb, buffer, size); yading@11: yading@11: packet_size = s->packet_size - size; yading@11: yading@11: if (s->is_vcd && (id & 0xe0) == AUDIO_ID) yading@11: /* The VCD standard demands that 20 zero bytes follow yading@11: each audio pack (see standard p. IV-8).*/ yading@11: zero_trail_bytes += 20; yading@11: yading@11: if ((s->is_vcd && stream->packet_number==0) yading@11: || (s->is_svcd && s->packet_number==0)) { yading@11: /* for VCD the first pack of each stream contains only the pack header, yading@11: the system header and lots of padding (see VCD standard p. IV-6). yading@11: In the case of an audio pack, 20 zero bytes are also added at yading@11: the end.*/ yading@11: /* For SVCD we fill the very first pack to increase compatibility with yading@11: some DVD players. Not mandated by the standard.*/ yading@11: if (s->is_svcd) yading@11: general_pack = 1; /* the system header refers to both streams and no stream data*/ yading@11: pad_packet_bytes = packet_size - zero_trail_bytes; yading@11: } yading@11: yading@11: packet_size -= pad_packet_bytes + zero_trail_bytes; yading@11: yading@11: if (packet_size > 0) { yading@11: yading@11: /* packet header size */ yading@11: packet_size -= 6; yading@11: yading@11: /* packet header */ yading@11: if (s->is_mpeg2) { yading@11: header_len = 3; yading@11: if (stream->packet_number==0) yading@11: header_len += 3; /* PES extension */ yading@11: header_len += 1; /* obligatory stuffing byte */ yading@11: } else { yading@11: header_len = 0; yading@11: } yading@11: if (pts != AV_NOPTS_VALUE) { yading@11: if (dts != pts) yading@11: header_len += 5 + 5; yading@11: else yading@11: header_len += 5; yading@11: } else { yading@11: if (!s->is_mpeg2) yading@11: header_len++; yading@11: } yading@11: yading@11: payload_size = packet_size - header_len; yading@11: if (id < 0xc0) { yading@11: startcode = PRIVATE_STREAM_1; yading@11: payload_size -= 1; yading@11: if (id >= 0x40) { yading@11: payload_size -= 3; yading@11: if (id >= 0xa0) yading@11: payload_size -= 3; yading@11: } yading@11: } else { yading@11: startcode = 0x100 + id; yading@11: } yading@11: yading@11: stuffing_size = payload_size - av_fifo_size(stream->fifo); yading@11: yading@11: // first byte does not fit -> reset pts/dts + stuffing yading@11: if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){ yading@11: int timestamp_len=0; yading@11: if(dts != pts) yading@11: timestamp_len += 5; yading@11: if(pts != AV_NOPTS_VALUE) yading@11: timestamp_len += s->is_mpeg2 ? 5 : 4; yading@11: pts=dts= AV_NOPTS_VALUE; yading@11: header_len -= timestamp_len; yading@11: if (s->is_dvd && stream->align_iframe) { yading@11: pad_packet_bytes += timestamp_len; yading@11: packet_size -= timestamp_len; yading@11: } else { yading@11: payload_size += timestamp_len; yading@11: } yading@11: stuffing_size += timestamp_len; yading@11: if(payload_size > trailer_size) yading@11: stuffing_size += payload_size - trailer_size; yading@11: } yading@11: yading@11: if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) { // can't use padding, so use stuffing yading@11: packet_size += pad_packet_bytes; yading@11: payload_size += pad_packet_bytes; // undo the previous adjustment yading@11: if (stuffing_size < 0) { yading@11: stuffing_size = pad_packet_bytes; yading@11: } else { yading@11: stuffing_size += pad_packet_bytes; yading@11: } yading@11: pad_packet_bytes = 0; yading@11: } yading@11: yading@11: if (stuffing_size < 0) yading@11: stuffing_size = 0; yading@11: yading@11: if (startcode == PRIVATE_STREAM_1 && id >= 0xa0) { yading@11: if (payload_size < av_fifo_size(stream->fifo)) yading@11: stuffing_size += payload_size % stream->lpcm_align; yading@11: } yading@11: yading@11: if (stuffing_size > 16) { /*<=16 for MPEG-1, <=32 for MPEG-2*/ yading@11: pad_packet_bytes += stuffing_size; yading@11: packet_size -= stuffing_size; yading@11: payload_size -= stuffing_size; yading@11: stuffing_size = 0; yading@11: } yading@11: yading@11: nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size); yading@11: yading@11: avio_wb32(ctx->pb, startcode); yading@11: yading@11: avio_wb16(ctx->pb, packet_size); yading@11: yading@11: if (!s->is_mpeg2) yading@11: for(i=0;ipb, 0xff); yading@11: yading@11: if (s->is_mpeg2) { yading@11: avio_w8(ctx->pb, 0x80); /* mpeg2 id */ yading@11: yading@11: pes_flags=0; yading@11: yading@11: if (pts != AV_NOPTS_VALUE) { yading@11: pes_flags |= 0x80; yading@11: if (dts != pts) yading@11: pes_flags |= 0x40; yading@11: } yading@11: yading@11: /* Both the MPEG-2 and the SVCD standards demand that the yading@11: P-STD_buffer_size field be included in the first packet of yading@11: every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2 yading@11: and MPEG-2 standard 2.7.7) */ yading@11: if (stream->packet_number == 0) yading@11: pes_flags |= 0x01; yading@11: yading@11: avio_w8(ctx->pb, pes_flags); /* flags */ yading@11: avio_w8(ctx->pb, header_len - 3 + stuffing_size); yading@11: yading@11: if (pes_flags & 0x80) /*write pts*/ yading@11: put_timestamp(ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts); yading@11: if (pes_flags & 0x40) /*write dts*/ yading@11: put_timestamp(ctx->pb, 0x01, dts); yading@11: yading@11: if (pes_flags & 0x01) { /*write pes extension*/ yading@11: avio_w8(ctx->pb, 0x10); /* flags */ yading@11: yading@11: /* P-STD buffer info */ yading@11: if ((id & 0xe0) == AUDIO_ID) yading@11: avio_wb16(ctx->pb, 0x4000 | stream->max_buffer_size/ 128); yading@11: else yading@11: avio_wb16(ctx->pb, 0x6000 | stream->max_buffer_size/1024); yading@11: } yading@11: yading@11: } else { yading@11: if (pts != AV_NOPTS_VALUE) { yading@11: if (dts != pts) { yading@11: put_timestamp(ctx->pb, 0x03, pts); yading@11: put_timestamp(ctx->pb, 0x01, dts); yading@11: } else { yading@11: put_timestamp(ctx->pb, 0x02, pts); yading@11: } yading@11: } else { yading@11: avio_w8(ctx->pb, 0x0f); yading@11: } yading@11: } yading@11: yading@11: if (s->is_mpeg2) { yading@11: /* special stuffing byte that is always written yading@11: to prevent accidental generation of start codes. */ yading@11: avio_w8(ctx->pb, 0xff); yading@11: yading@11: for(i=0;ipb, 0xff); yading@11: } yading@11: yading@11: if (startcode == PRIVATE_STREAM_1) { yading@11: avio_w8(ctx->pb, id); yading@11: if (id >= 0xa0) { yading@11: /* LPCM (XXX: check nb_frames) */ yading@11: avio_w8(ctx->pb, 7); yading@11: avio_wb16(ctx->pb, 4); /* skip 3 header bytes */ yading@11: avio_w8(ctx->pb, stream->lpcm_header[0]); yading@11: avio_w8(ctx->pb, stream->lpcm_header[1]); yading@11: avio_w8(ctx->pb, stream->lpcm_header[2]); yading@11: } else if (id >= 0x40) { yading@11: /* AC-3 */ yading@11: avio_w8(ctx->pb, nb_frames); yading@11: avio_wb16(ctx->pb, trailer_size+1); yading@11: } yading@11: } yading@11: yading@11: /* output data */ yading@11: assert(payload_size - stuffing_size <= av_fifo_size(stream->fifo)); yading@11: av_fifo_generic_read(stream->fifo, ctx->pb, payload_size - stuffing_size, (void*)avio_write); yading@11: stream->bytes_to_iframe -= payload_size - stuffing_size; yading@11: }else{ yading@11: payload_size= yading@11: stuffing_size= 0; yading@11: } yading@11: yading@11: if (pad_packet_bytes > 0) yading@11: put_padding_packet(ctx,ctx->pb, pad_packet_bytes); yading@11: yading@11: for(i=0;ipb, 0x00); yading@11: yading@11: avio_flush(ctx->pb); yading@11: yading@11: s->packet_number++; yading@11: yading@11: /* only increase the stream packet number if this pack actually contains yading@11: something that is specific to this stream! I.e. a dedicated header yading@11: or some data.*/ yading@11: if (!general_pack) yading@11: stream->packet_number++; yading@11: yading@11: return payload_size - stuffing_size; yading@11: } yading@11: yading@11: static void put_vcd_padding_sector(AVFormatContext *ctx) yading@11: { yading@11: /* There are two ways to do this padding: writing a sector/pack yading@11: of 0 values, or writing an MPEG padding pack. Both seem to yading@11: work with most decoders, BUT the VCD standard only allows a 0-sector yading@11: (see standard p. IV-4, IV-5). yading@11: So a 0-sector it is...*/ yading@11: yading@11: MpegMuxContext *s = ctx->priv_data; yading@11: int i; yading@11: yading@11: for(i=0;ipacket_size;i++) yading@11: avio_w8(ctx->pb, 0); yading@11: yading@11: s->vcd_padding_bytes_written += s->packet_size; yading@11: yading@11: avio_flush(ctx->pb); yading@11: yading@11: /* increasing the packet number is correct. The SCR of the following packs yading@11: is calculated from the packet_number and it has to include the padding yading@11: sector (it represents the sector index, not the MPEG pack index) yading@11: (see VCD standard p. IV-6)*/ yading@11: s->packet_number++; yading@11: } yading@11: yading@11: static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){ yading@11: // MpegMuxContext *s = ctx->priv_data; yading@11: int i; yading@11: yading@11: for(i=0; inb_streams; i++){ yading@11: AVStream *st = ctx->streams[i]; yading@11: StreamInfo *stream = st->priv_data; yading@11: PacketDesc *pkt_desc; yading@11: yading@11: while((pkt_desc= stream->predecode_packet) yading@11: && scr > pkt_desc->dts){ //FIXME > vs >= yading@11: if(stream->buffer_index < pkt_desc->size || yading@11: stream->predecode_packet == stream->premux_packet){ yading@11: av_log(ctx, AV_LOG_ERROR, yading@11: "buffer underflow i=%d bufi=%d size=%d\n", yading@11: i, stream->buffer_index, pkt_desc->size); yading@11: break; yading@11: } yading@11: stream->buffer_index -= pkt_desc->size; yading@11: yading@11: stream->predecode_packet= pkt_desc->next; yading@11: av_freep(&pkt_desc); yading@11: } yading@11: } yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: static int output_packet(AVFormatContext *ctx, int flush){ yading@11: MpegMuxContext *s = ctx->priv_data; yading@11: AVStream *st; yading@11: StreamInfo *stream; yading@11: int i, avail_space=0, es_size, trailer_size; yading@11: int best_i= -1; yading@11: int best_score= INT_MIN; yading@11: int ignore_constraints=0; yading@11: int64_t scr= s->last_scr; yading@11: PacketDesc *timestamp_packet; yading@11: const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE); yading@11: yading@11: retry: yading@11: for(i=0; inb_streams; i++){ yading@11: AVStream *st = ctx->streams[i]; yading@11: StreamInfo *stream = st->priv_data; yading@11: const int avail_data= av_fifo_size(stream->fifo); yading@11: const int space= stream->max_buffer_size - stream->buffer_index; yading@11: int rel_space= 1024LL*space / stream->max_buffer_size; yading@11: PacketDesc *next_pkt= stream->premux_packet; yading@11: yading@11: /* for subtitle, a single PES packet must be generated, yading@11: so we flush after every single subtitle packet */ yading@11: if(s->packet_size > avail_data && !flush yading@11: && st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE) yading@11: return 0; yading@11: if(avail_data==0) yading@11: continue; yading@11: assert(avail_data>0); yading@11: yading@11: if(space < s->packet_size && !ignore_constraints) yading@11: continue; yading@11: yading@11: if(next_pkt && next_pkt->dts - scr > max_delay) yading@11: continue; yading@11: yading@11: if(rel_space > best_score){ yading@11: best_score= rel_space; yading@11: best_i = i; yading@11: avail_space= space; yading@11: } yading@11: } yading@11: yading@11: if(best_i < 0){ yading@11: int64_t best_dts= INT64_MAX; yading@11: yading@11: for(i=0; inb_streams; i++){ yading@11: AVStream *st = ctx->streams[i]; yading@11: StreamInfo *stream = st->priv_data; yading@11: PacketDesc *pkt_desc= stream->predecode_packet; yading@11: if(pkt_desc && pkt_desc->dts < best_dts) yading@11: best_dts= pkt_desc->dts; yading@11: } yading@11: yading@11: av_dlog(ctx, "bumping scr, scr:%f, dts:%f\n", yading@11: scr / 90000.0, best_dts / 90000.0); yading@11: if(best_dts == INT64_MAX) yading@11: return 0; yading@11: yading@11: if(scr >= best_dts+1 && !ignore_constraints){ yading@11: av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n"); yading@11: ignore_constraints= 1; yading@11: } yading@11: scr= FFMAX(best_dts+1, scr); yading@11: if(remove_decoded_packets(ctx, scr) < 0) yading@11: return -1; yading@11: goto retry; yading@11: } yading@11: yading@11: assert(best_i >= 0); yading@11: yading@11: st = ctx->streams[best_i]; yading@11: stream = st->priv_data; yading@11: yading@11: assert(av_fifo_size(stream->fifo) > 0); yading@11: yading@11: assert(avail_space >= s->packet_size || ignore_constraints); yading@11: yading@11: timestamp_packet= stream->premux_packet; yading@11: if(timestamp_packet->unwritten_size == timestamp_packet->size){ yading@11: trailer_size= 0; yading@11: }else{ yading@11: trailer_size= timestamp_packet->unwritten_size; yading@11: timestamp_packet= timestamp_packet->next; yading@11: } yading@11: yading@11: if(timestamp_packet){ yading@11: av_dlog(ctx, "dts:%f pts:%f scr:%f stream:%d\n", yading@11: timestamp_packet->dts / 90000.0, yading@11: timestamp_packet->pts / 90000.0, yading@11: scr / 90000.0, best_i); yading@11: es_size= flush_packet(ctx, best_i, timestamp_packet->pts, timestamp_packet->dts, scr, trailer_size); yading@11: }else{ yading@11: assert(av_fifo_size(stream->fifo) == trailer_size); yading@11: es_size= flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr, trailer_size); yading@11: } yading@11: yading@11: if (s->is_vcd) { yading@11: /* Write one or more padding sectors, if necessary, to reach yading@11: the constant overall bitrate.*/ yading@11: int vcd_pad_bytes; yading@11: yading@11: while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->premux_packet->pts) ) >= s->packet_size){ //FIXME pts cannot be correct here yading@11: put_vcd_padding_sector(ctx); yading@11: s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet yading@11: } yading@11: } yading@11: yading@11: stream->buffer_index += es_size; yading@11: s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet yading@11: yading@11: while(stream->premux_packet && stream->premux_packet->unwritten_size <= es_size){ yading@11: es_size -= stream->premux_packet->unwritten_size; yading@11: stream->premux_packet= stream->premux_packet->next; yading@11: } yading@11: if(es_size) yading@11: stream->premux_packet->unwritten_size -= es_size; yading@11: yading@11: if(remove_decoded_packets(ctx, s->last_scr) < 0) yading@11: return -1; yading@11: yading@11: return 1; yading@11: } yading@11: yading@11: static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt) yading@11: { yading@11: MpegMuxContext *s = ctx->priv_data; yading@11: int stream_index= pkt->stream_index; yading@11: int size= pkt->size; yading@11: uint8_t *buf= pkt->data; yading@11: AVStream *st = ctx->streams[stream_index]; yading@11: StreamInfo *stream = st->priv_data; yading@11: int64_t pts, dts; yading@11: PacketDesc *pkt_desc; yading@11: int preload; yading@11: const int is_iframe = st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (pkt->flags & AV_PKT_FLAG_KEY); yading@11: yading@11: preload = av_rescale(s->preload, 90000, AV_TIME_BASE); yading@11: yading@11: pts= pkt->pts; yading@11: dts= pkt->dts; yading@11: yading@11: if (s->last_scr == AV_NOPTS_VALUE) { yading@11: if (dts == AV_NOPTS_VALUE || (dts < preload && ctx->avoid_negative_ts) || s->is_dvd) { yading@11: if (dts != AV_NOPTS_VALUE) yading@11: s->preload += av_rescale(-dts, AV_TIME_BASE, 90000); yading@11: s->last_scr = 0; yading@11: } else { yading@11: s->last_scr = dts - preload; yading@11: s->preload = 0; yading@11: } yading@11: preload = av_rescale(s->preload, 90000, AV_TIME_BASE); yading@11: av_log(ctx, AV_LOG_DEBUG, "First SCR: %"PRId64" First DTS: %"PRId64"\n", s->last_scr, dts + preload); yading@11: } yading@11: yading@11: if (dts != AV_NOPTS_VALUE) dts += preload; yading@11: if (pts != AV_NOPTS_VALUE) pts += preload; yading@11: yading@11: av_dlog(ctx, "dts:%f pts:%f flags:%d stream:%d nopts:%d\n", yading@11: dts / 90000.0, pts / 90000.0, pkt->flags, yading@11: pkt->stream_index, pts != AV_NOPTS_VALUE); yading@11: if (!stream->premux_packet) yading@11: stream->next_packet = &stream->premux_packet; yading@11: *stream->next_packet= yading@11: pkt_desc= av_mallocz(sizeof(PacketDesc)); yading@11: pkt_desc->pts= pts; yading@11: pkt_desc->dts= dts; yading@11: pkt_desc->unwritten_size= yading@11: pkt_desc->size= size; yading@11: if(!stream->predecode_packet) yading@11: stream->predecode_packet= pkt_desc; yading@11: stream->next_packet= &pkt_desc->next; yading@11: yading@11: if (av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size) < 0) yading@11: return -1; yading@11: yading@11: if (s->is_dvd){ yading@11: if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) { // min VOBU length 0.4 seconds (mpucoder) yading@11: stream->bytes_to_iframe = av_fifo_size(stream->fifo); yading@11: stream->align_iframe = 1; yading@11: stream->vobu_start_pts = pts; yading@11: } yading@11: } yading@11: yading@11: av_fifo_generic_write(stream->fifo, buf, size, NULL); yading@11: yading@11: for(;;){ yading@11: int ret= output_packet(ctx, 0); yading@11: if(ret<=0) yading@11: return ret; yading@11: } yading@11: } yading@11: yading@11: static int mpeg_mux_end(AVFormatContext *ctx) yading@11: { yading@11: // MpegMuxContext *s = ctx->priv_data; yading@11: StreamInfo *stream; yading@11: int i; yading@11: yading@11: for(;;){ yading@11: int ret= output_packet(ctx, 1); yading@11: if(ret<0) yading@11: return ret; yading@11: else if(ret==0) yading@11: break; yading@11: } yading@11: yading@11: /* End header according to MPEG1 systems standard. We do not write yading@11: it as it is usually not needed by decoders and because it yading@11: complicates MPEG stream concatenation. */ yading@11: //avio_wb32(ctx->pb, ISO_11172_END_CODE); yading@11: //avio_flush(ctx->pb); yading@11: yading@11: for(i=0;inb_streams;i++) { yading@11: stream = ctx->streams[i]->priv_data; yading@11: yading@11: assert(av_fifo_size(stream->fifo) == 0); yading@11: av_fifo_free(stream->fifo); yading@11: } yading@11: return 0; yading@11: } yading@11: yading@11: #define OFFSET(x) offsetof(MpegMuxContext, x) yading@11: #define E AV_OPT_FLAG_ENCODING_PARAM yading@11: static const AVOption options[] = { yading@11: { "muxrate", NULL, OFFSET(user_mux_rate), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, E }, yading@11: { "preload", "Initial demux-decode delay in microseconds.", OFFSET(preload), AV_OPT_TYPE_INT, {.i64 = 500000}, 0, INT_MAX, E}, yading@11: { NULL }, yading@11: }; yading@11: yading@11: #define MPEGENC_CLASS(flavor)\ yading@11: static const AVClass flavor ## _class = {\ yading@11: .class_name = #flavor " muxer",\ yading@11: .item_name = av_default_item_name,\ yading@11: .version = LIBAVUTIL_VERSION_INT,\ yading@11: .option = options,\ yading@11: }; yading@11: yading@11: #if CONFIG_MPEG1SYSTEM_MUXER yading@11: MPEGENC_CLASS(mpeg) yading@11: AVOutputFormat ff_mpeg1system_muxer = { yading@11: .name = "mpeg", yading@11: .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream"), yading@11: .mime_type = "video/mpeg", yading@11: .extensions = "mpg,mpeg", yading@11: .priv_data_size = sizeof(MpegMuxContext), yading@11: .audio_codec = AV_CODEC_ID_MP2, yading@11: .video_codec = AV_CODEC_ID_MPEG1VIDEO, yading@11: .write_header = mpeg_mux_init, yading@11: .write_packet = mpeg_mux_write_packet, yading@11: .write_trailer = mpeg_mux_end, yading@11: .priv_class = &mpeg_class, yading@11: }; yading@11: #endif yading@11: #if CONFIG_MPEG1VCD_MUXER yading@11: MPEGENC_CLASS(vcd) yading@11: AVOutputFormat ff_mpeg1vcd_muxer = { yading@11: .name = "vcd", yading@11: .long_name = NULL_IF_CONFIG_SMALL("MPEG-1 Systems / MPEG program stream (VCD)"), yading@11: .mime_type = "video/mpeg", yading@11: .priv_data_size = sizeof(MpegMuxContext), yading@11: .audio_codec = AV_CODEC_ID_MP2, yading@11: .video_codec = AV_CODEC_ID_MPEG1VIDEO, yading@11: .write_header = mpeg_mux_init, yading@11: .write_packet = mpeg_mux_write_packet, yading@11: .write_trailer = mpeg_mux_end, yading@11: .priv_class = &vcd_class, yading@11: }; yading@11: #endif yading@11: #if CONFIG_MPEG2VOB_MUXER yading@11: MPEGENC_CLASS(vob) yading@11: AVOutputFormat ff_mpeg2vob_muxer = { yading@11: .name = "vob", yading@11: .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (VOB)"), yading@11: .mime_type = "video/mpeg", yading@11: .extensions = "vob", yading@11: .priv_data_size = sizeof(MpegMuxContext), yading@11: .audio_codec = AV_CODEC_ID_MP2, yading@11: .video_codec = AV_CODEC_ID_MPEG2VIDEO, yading@11: .write_header = mpeg_mux_init, yading@11: .write_packet = mpeg_mux_write_packet, yading@11: .write_trailer = mpeg_mux_end, yading@11: .priv_class = &vob_class, yading@11: }; yading@11: #endif yading@11: yading@11: /* Same as mpeg2vob_mux except that the pack size is 2324 */ yading@11: #if CONFIG_MPEG2SVCD_MUXER yading@11: MPEGENC_CLASS(svcd) yading@11: AVOutputFormat ff_mpeg2svcd_muxer = { yading@11: .name = "svcd", yading@11: .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (SVCD)"), yading@11: .mime_type = "video/mpeg", yading@11: .extensions = "vob", yading@11: .priv_data_size = sizeof(MpegMuxContext), yading@11: .audio_codec = AV_CODEC_ID_MP2, yading@11: .video_codec = AV_CODEC_ID_MPEG2VIDEO, yading@11: .write_header = mpeg_mux_init, yading@11: .write_packet = mpeg_mux_write_packet, yading@11: .write_trailer = mpeg_mux_end, yading@11: .priv_class = &svcd_class, yading@11: }; yading@11: #endif yading@11: yading@11: /* Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */ yading@11: #if CONFIG_MPEG2DVD_MUXER yading@11: MPEGENC_CLASS(dvd) yading@11: AVOutputFormat ff_mpeg2dvd_muxer = { yading@11: .name = "dvd", yading@11: .long_name = NULL_IF_CONFIG_SMALL("MPEG-2 PS (DVD VOB)"), yading@11: .mime_type = "video/mpeg", yading@11: .extensions = "dvd", yading@11: .priv_data_size = sizeof(MpegMuxContext), yading@11: .audio_codec = AV_CODEC_ID_MP2, yading@11: .video_codec = AV_CODEC_ID_MPEG2VIDEO, yading@11: .write_header = mpeg_mux_init, yading@11: .write_packet = mpeg_mux_write_packet, yading@11: .write_trailer = mpeg_mux_end, yading@11: .priv_class = &dvd_class, yading@11: }; yading@11: #endif