• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/mpegenc.c

Go to the documentation of this file.
00001 /*
00002  * MPEG1/2 muxer
00003  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "libavutil/fifo.h"
00023 #include "libavcodec/put_bits.h"
00024 #include "avformat.h"
00025 #include "mpeg.h"
00026 
00027 #define MAX_PAYLOAD_SIZE 4096
00028 
00029 #undef NDEBUG
00030 #include <assert.h>
00031 
00032 typedef struct PacketDesc {
00033     int64_t pts;
00034     int64_t dts;
00035     int size;
00036     int unwritten_size;
00037     int flags;
00038     struct PacketDesc *next;
00039 } PacketDesc;
00040 
00041 typedef struct {
00042     AVFifoBuffer *fifo;
00043     uint8_t id;
00044     int max_buffer_size; /* in bytes */
00045     int buffer_index;
00046     PacketDesc *predecode_packet;
00047     PacketDesc *premux_packet;
00048     PacketDesc **next_packet;
00049     int packet_number;
00050     uint8_t lpcm_header[3];
00051     int lpcm_align;
00052     int bytes_to_iframe;
00053     int align_iframe;
00054     int64_t vobu_start_pts;
00055 } StreamInfo;
00056 
00057 typedef struct {
00058     int packet_size; /* required packet size */
00059     int packet_number;
00060     int pack_header_freq;     /* frequency (in packets^-1) at which we send pack headers */
00061     int system_header_freq;
00062     int system_header_size;
00063     int mux_rate; /* bitrate in units of 50 bytes/s */
00064     /* stream info */
00065     int audio_bound;
00066     int video_bound;
00067     int is_mpeg2;
00068     int is_vcd;
00069     int is_svcd;
00070     int is_dvd;
00071     int64_t last_scr; /* current system clock */
00072 
00073     double vcd_padding_bitrate; //FIXME floats
00074     int64_t vcd_padding_bytes_written;
00075 
00076 } MpegMuxContext;
00077 
00078 extern AVOutputFormat ff_mpeg1vcd_muxer;
00079 extern AVOutputFormat ff_mpeg2dvd_muxer;
00080 extern AVOutputFormat ff_mpeg2svcd_muxer;
00081 extern AVOutputFormat ff_mpeg2vob_muxer;
00082 
00083 static int put_pack_header(AVFormatContext *ctx,
00084                            uint8_t *buf, int64_t timestamp)
00085 {
00086     MpegMuxContext *s = ctx->priv_data;
00087     PutBitContext pb;
00088 
00089     init_put_bits(&pb, buf, 128);
00090 
00091     put_bits32(&pb, PACK_START_CODE);
00092     if (s->is_mpeg2) {
00093         put_bits(&pb, 2, 0x1);
00094     } else {
00095         put_bits(&pb, 4, 0x2);
00096     }
00097     put_bits(&pb, 3,  (uint32_t)((timestamp >> 30) & 0x07));
00098     put_bits(&pb, 1, 1);
00099     put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
00100     put_bits(&pb, 1, 1);
00101     put_bits(&pb, 15, (uint32_t)((timestamp      ) & 0x7fff));
00102     put_bits(&pb, 1, 1);
00103     if (s->is_mpeg2) {
00104         /* clock extension */
00105         put_bits(&pb, 9, 0);
00106     }
00107     put_bits(&pb, 1, 1);
00108     put_bits(&pb, 22, s->mux_rate);
00109     put_bits(&pb, 1, 1);
00110     if (s->is_mpeg2) {
00111         put_bits(&pb, 1, 1);
00112         put_bits(&pb, 5, 0x1f); /* reserved */
00113         put_bits(&pb, 3, 0); /* stuffing length */
00114     }
00115     flush_put_bits(&pb);
00116     return put_bits_ptr(&pb) - pb.buf;
00117 }
00118 
00119 static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id)
00120 {
00121     MpegMuxContext *s = ctx->priv_data;
00122     int size, i, private_stream_coded, id;
00123     PutBitContext pb;
00124 
00125     init_put_bits(&pb, buf, 128);
00126 
00127     put_bits32(&pb, SYSTEM_HEADER_START_CODE);
00128     put_bits(&pb, 16, 0);
00129     put_bits(&pb, 1, 1);
00130 
00131     put_bits(&pb, 22, s->mux_rate); /* maximum bit rate of the multiplexed stream */
00132     put_bits(&pb, 1, 1); /* marker */
00133     if (s->is_vcd && only_for_stream_id==VIDEO_ID) {
00134         /* This header applies only to the video stream (see VCD standard p. IV-7)*/
00135         put_bits(&pb, 6, 0);
00136     } else
00137         put_bits(&pb, 6, s->audio_bound);
00138 
00139     if (s->is_vcd) {
00140         /* see VCD standard, p. IV-7*/
00141         put_bits(&pb, 1, 0);
00142         put_bits(&pb, 1, 1);
00143     } else {
00144         put_bits(&pb, 1, 0); /* variable bitrate*/
00145         put_bits(&pb, 1, 0); /* non constrainted bit stream */
00146     }
00147 
00148     if (s->is_vcd || s->is_dvd) {
00149         /* see VCD standard p IV-7 */
00150         put_bits(&pb, 1, 1); /* audio locked */
00151         put_bits(&pb, 1, 1); /* video locked */
00152     } else {
00153         put_bits(&pb, 1, 0); /* audio locked */
00154         put_bits(&pb, 1, 0); /* video locked */
00155     }
00156 
00157     put_bits(&pb, 1, 1); /* marker */
00158 
00159     if (s->is_vcd && (only_for_stream_id & 0xe0) == AUDIO_ID) {
00160         /* This header applies only to the audio stream (see VCD standard p. IV-7)*/
00161         put_bits(&pb, 5, 0);
00162     } else
00163         put_bits(&pb, 5, s->video_bound);
00164 
00165     if (s->is_dvd) {
00166         put_bits(&pb, 1, 0);    /* packet_rate_restriction_flag */
00167         put_bits(&pb, 7, 0x7f); /* reserved byte */
00168     } else
00169         put_bits(&pb, 8, 0xff); /* reserved byte */
00170 
00171     /* DVD-Video Stream_bound entries
00172     id (0xB9) video, maximum P-STD for stream 0xE0. (P-STD_buffer_bound_scale = 1)
00173     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)
00174     id (0xBD) private stream 1 (audio other than MPEG and subpictures). (P-STD_buffer_bound_scale = 1)
00175     id (0xBF) private stream 2, NAV packs, set to 2x1024. */
00176     if (s->is_dvd) {
00177 
00178         int P_STD_max_video = 0;
00179         int P_STD_max_mpeg_audio = 0;
00180         int P_STD_max_mpeg_PS1 = 0;
00181 
00182         for(i=0;i<ctx->nb_streams;i++) {
00183             StreamInfo *stream = ctx->streams[i]->priv_data;
00184 
00185             id = stream->id;
00186             if (id == 0xbd && stream->max_buffer_size > P_STD_max_mpeg_PS1) {
00187                 P_STD_max_mpeg_PS1 = stream->max_buffer_size;
00188             } else if (id >= 0xc0 && id <= 0xc7 && stream->max_buffer_size > P_STD_max_mpeg_audio) {
00189                 P_STD_max_mpeg_audio = stream->max_buffer_size;
00190             } else if (id == 0xe0 && stream->max_buffer_size > P_STD_max_video) {
00191                 P_STD_max_video = stream->max_buffer_size;
00192             }
00193         }
00194 
00195         /* video */
00196         put_bits(&pb, 8, 0xb9); /* stream ID */
00197         put_bits(&pb, 2, 3);
00198         put_bits(&pb, 1, 1);
00199         put_bits(&pb, 13, P_STD_max_video / 1024);
00200 
00201         /* audio */
00202         if (P_STD_max_mpeg_audio == 0)
00203             P_STD_max_mpeg_audio = 4096;
00204         put_bits(&pb, 8, 0xb8); /* stream ID */
00205         put_bits(&pb, 2, 3);
00206         put_bits(&pb, 1, 0);
00207         put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
00208 
00209         /* private stream 1 */
00210         put_bits(&pb, 8, 0xbd); /* stream ID */
00211         put_bits(&pb, 2, 3);
00212         put_bits(&pb, 1, 0);
00213         put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
00214 
00215         /* private stream 2 */
00216         put_bits(&pb, 8, 0xbf); /* stream ID */
00217         put_bits(&pb, 2, 3);
00218         put_bits(&pb, 1, 1);
00219         put_bits(&pb, 13, 2);
00220     }
00221     else {
00222         /* audio stream info */
00223         private_stream_coded = 0;
00224         for(i=0;i<ctx->nb_streams;i++) {
00225             StreamInfo *stream = ctx->streams[i]->priv_data;
00226 
00227 
00228             /* For VCDs, only include the stream info for the stream
00229             that the pack which contains this system belongs to.
00230             (see VCD standard p. IV-7) */
00231             if ( !s->is_vcd || stream->id==only_for_stream_id
00232                 || only_for_stream_id==0) {
00233 
00234                 id = stream->id;
00235                 if (id < 0xc0) {
00236                     /* special case for private streams (AC-3 uses that) */
00237                     if (private_stream_coded)
00238                         continue;
00239                     private_stream_coded = 1;
00240                     id = 0xbd;
00241                 }
00242                 put_bits(&pb, 8, id); /* stream ID */
00243                 put_bits(&pb, 2, 3);
00244                 if (id < 0xe0) {
00245                     /* audio */
00246                     put_bits(&pb, 1, 0);
00247                     put_bits(&pb, 13, stream->max_buffer_size / 128);
00248                 } else {
00249                     /* video */
00250                     put_bits(&pb, 1, 1);
00251                     put_bits(&pb, 13, stream->max_buffer_size / 1024);
00252                 }
00253             }
00254         }
00255     }
00256 
00257     flush_put_bits(&pb);
00258     size = put_bits_ptr(&pb) - pb.buf;
00259     /* patch packet size */
00260     buf[4] = (size - 6) >> 8;
00261     buf[5] = (size - 6) & 0xff;
00262 
00263     return size;
00264 }
00265 
00266 static int get_system_header_size(AVFormatContext *ctx)
00267 {
00268     int buf_index, i, private_stream_coded;
00269     StreamInfo *stream;
00270     MpegMuxContext *s = ctx->priv_data;
00271 
00272     if (s->is_dvd)
00273        return 18; // DVD-Video system headers are 18 bytes fixed length.
00274 
00275     buf_index = 12;
00276     private_stream_coded = 0;
00277     for(i=0;i<ctx->nb_streams;i++) {
00278         stream = ctx->streams[i]->priv_data;
00279         if (stream->id < 0xc0) {
00280             if (private_stream_coded)
00281                 continue;
00282             private_stream_coded = 1;
00283         }
00284         buf_index += 3;
00285     }
00286     return buf_index;
00287 }
00288 
00289 static int mpeg_mux_init(AVFormatContext *ctx)
00290 {
00291     MpegMuxContext *s = ctx->priv_data;
00292     int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j;
00293     AVStream *st;
00294     StreamInfo *stream;
00295     int audio_bitrate;
00296     int video_bitrate;
00297 
00298     s->packet_number = 0;
00299     s->is_vcd =    (CONFIG_MPEG1VCD_MUXER  && ctx->oformat == &ff_mpeg1vcd_muxer);
00300     s->is_svcd =   (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer);
00301     s->is_mpeg2 = ((CONFIG_MPEG2VOB_MUXER  && ctx->oformat == &ff_mpeg2vob_muxer) ||
00302                    (CONFIG_MPEG2DVD_MUXER  && ctx->oformat == &ff_mpeg2dvd_muxer) ||
00303                    (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer));
00304     s->is_dvd =    (CONFIG_MPEG2DVD_MUXER  && ctx->oformat == &ff_mpeg2dvd_muxer);
00305 
00306     if(ctx->packet_size) {
00307         if (ctx->packet_size < 20 || ctx->packet_size > (1 << 23) + 10) {
00308             av_log(ctx, AV_LOG_ERROR, "Invalid packet size %d\n",
00309                    ctx->packet_size);
00310             goto fail;
00311         }
00312         s->packet_size = ctx->packet_size;
00313     } else
00314         s->packet_size = 2048;
00315 
00316     s->vcd_padding_bytes_written = 0;
00317     s->vcd_padding_bitrate=0;
00318 
00319     s->audio_bound = 0;
00320     s->video_bound = 0;
00321     mpa_id = AUDIO_ID;
00322     ac3_id = AC3_ID;
00323     dts_id = DTS_ID;
00324     mpv_id = VIDEO_ID;
00325     mps_id = SUB_ID;
00326     lpcm_id = LPCM_ID;
00327     for(i=0;i<ctx->nb_streams;i++) {
00328         st = ctx->streams[i];
00329         stream = av_mallocz(sizeof(StreamInfo));
00330         if (!stream)
00331             goto fail;
00332         st->priv_data = stream;
00333 
00334         av_set_pts_info(st, 64, 1, 90000);
00335 
00336         switch(st->codec->codec_type) {
00337         case AVMEDIA_TYPE_AUDIO:
00338             if        (st->codec->codec_id == CODEC_ID_AC3) {
00339                 stream->id = ac3_id++;
00340             } else if (st->codec->codec_id == CODEC_ID_DTS) {
00341                 stream->id = dts_id++;
00342             } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
00343                 stream->id = lpcm_id++;
00344                 for(j = 0; j < 4; j++) {
00345                     if (lpcm_freq_tab[j] == st->codec->sample_rate)
00346                         break;
00347                 }
00348                 if (j == 4)
00349                     goto fail;
00350                 if (st->codec->channels > 8)
00351                     return -1;
00352                 stream->lpcm_header[0] = 0x0c;
00353                 stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4);
00354                 stream->lpcm_header[2] = 0x80;
00355                 stream->lpcm_align = st->codec->channels * 2;
00356             } else {
00357                 stream->id = mpa_id++;
00358             }
00359 
00360             /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
00361                Right now it is also used for everything else.*/
00362             stream->max_buffer_size = 4 * 1024;
00363             s->audio_bound++;
00364             break;
00365         case AVMEDIA_TYPE_VIDEO:
00366             stream->id = mpv_id++;
00367             if (st->codec->rc_buffer_size)
00368                 stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8;
00369             else {
00370                 av_log(ctx, AV_LOG_WARNING, "VBV buffer size not set, muxing may fail\n");
00371                 stream->max_buffer_size = 230*1024; //FIXME this is probably too small as default
00372             }
00373 #if 0
00374                 /* see VCD standard, p. IV-7*/
00375                 stream->max_buffer_size = 46 * 1024;
00376             else
00377                 /* This value HAS to be used for SVCD (see SVCD standard, p. 26 V.2.3.2).
00378                    Right now it is also used for everything else.*/
00379                 stream->max_buffer_size = 230 * 1024;
00380 #endif
00381             s->video_bound++;
00382             break;
00383         case AVMEDIA_TYPE_SUBTITLE:
00384             stream->id = mps_id++;
00385             stream->max_buffer_size = 16 * 1024;
00386             break;
00387         default:
00388             return -1;
00389         }
00390         stream->fifo= av_fifo_alloc(16);
00391         if (!stream->fifo)
00392             goto fail;
00393     }
00394     bitrate = 0;
00395     audio_bitrate = 0;
00396     video_bitrate = 0;
00397     for(i=0;i<ctx->nb_streams;i++) {
00398         int codec_rate;
00399         st = ctx->streams[i];
00400         stream = (StreamInfo*) st->priv_data;
00401 
00402         if(st->codec->rc_max_rate || stream->id==VIDEO_ID)
00403             codec_rate= st->codec->rc_max_rate;
00404         else
00405             codec_rate= st->codec->bit_rate;
00406 
00407         if(!codec_rate)
00408             codec_rate= (1<<21)*8*50/ctx->nb_streams;
00409 
00410         bitrate += codec_rate;
00411 
00412         if ((stream->id & 0xe0) == AUDIO_ID)
00413             audio_bitrate += codec_rate;
00414         else if (stream->id==VIDEO_ID)
00415             video_bitrate += codec_rate;
00416     }
00417 
00418     if(ctx->mux_rate){
00419         s->mux_rate= (ctx->mux_rate + (8 * 50) - 1) / (8 * 50);
00420     } else {
00421         /* we increase slightly the bitrate to take into account the
00422            headers. XXX: compute it exactly */
00423         bitrate += bitrate*5/100;
00424         bitrate += 10000;
00425         s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
00426     }
00427 
00428     if (s->is_vcd) {
00429         double overhead_rate;
00430 
00431         /* The VCD standard mandates that the mux_rate field is 3528
00432            (see standard p. IV-6).
00433            The value is actually "wrong", i.e. if you calculate
00434            it using the normal formula and the 75 sectors per second transfer
00435            rate you get a different value because the real pack size is 2324,
00436            not 2352. But the standard explicitly specifies that the mux_rate
00437            field in the header must have this value.*/
00438 //        s->mux_rate=2352 * 75 / 50;    /* = 3528*/
00439 
00440         /* The VCD standard states that the muxed stream must be
00441            exactly 75 packs / second (the data rate of a single speed cdrom).
00442            Since the video bitrate (probably 1150000 bits/sec) will be below
00443            the theoretical maximum we have to add some padding packets
00444            to make up for the lower data rate.
00445            (cf. VCD standard p. IV-6 )*/
00446 
00447         /* Add the header overhead to the data rate.
00448            2279 data bytes per audio pack, 2294 data bytes per video pack*/
00449         overhead_rate  = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
00450         overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
00451         overhead_rate *= 8;
00452 
00453         /* Add padding so that the full bitrate is 2324*75 bytes/sec */
00454         s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate);
00455     }
00456 
00457     if (s->is_vcd || s->is_mpeg2)
00458         /* every packet */
00459         s->pack_header_freq = 1;
00460     else
00461         /* every 2 seconds */
00462         s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
00463 
00464     /* the above seems to make pack_header_freq zero sometimes */
00465     if (s->pack_header_freq == 0)
00466        s->pack_header_freq = 1;
00467 
00468     if (s->is_mpeg2)
00469         /* every 200 packets. Need to look at the spec.  */
00470         s->system_header_freq = s->pack_header_freq * 40;
00471     else if (s->is_vcd)
00472         /* the standard mandates that there are only two system headers
00473            in the whole file: one in the first packet of each stream.
00474            (see standard p. IV-7 and IV-8) */
00475         s->system_header_freq = 0x7fffffff;
00476     else
00477         s->system_header_freq = s->pack_header_freq * 5;
00478 
00479     for(i=0;i<ctx->nb_streams;i++) {
00480         stream = ctx->streams[i]->priv_data;
00481         stream->packet_number = 0;
00482     }
00483     s->system_header_size = get_system_header_size(ctx);
00484     s->last_scr = 0;
00485     return 0;
00486  fail:
00487     for(i=0;i<ctx->nb_streams;i++) {
00488         av_free(ctx->streams[i]->priv_data);
00489     }
00490     return AVERROR(ENOMEM);
00491 }
00492 
00493 static inline void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
00494 {
00495     avio_w8(pb,
00496              (id << 4) |
00497              (((timestamp >> 30) & 0x07) << 1) |
00498              1);
00499     avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
00500     avio_wb16(pb, (uint16_t)((((timestamp      ) & 0x7fff) << 1) | 1));
00501 }
00502 
00503 
00504 /* return the number of padding bytes that should be inserted into
00505    the multiplexed stream.*/
00506 static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
00507 {
00508     MpegMuxContext *s = ctx->priv_data;
00509     int pad_bytes = 0;
00510 
00511     if (s->vcd_padding_bitrate > 0 && pts!=AV_NOPTS_VALUE)
00512     {
00513         int64_t full_pad_bytes;
00514 
00515         full_pad_bytes = (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0); //FIXME this is wrong
00516         pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written);
00517 
00518         if (pad_bytes<0)
00519             /* might happen if we have already padded to a later timestamp. This
00520                can occur if another stream has already advanced further.*/
00521             pad_bytes=0;
00522     }
00523 
00524     return pad_bytes;
00525 }
00526 
00527 
00528 #if 0 /* unused, remove? */
00529 /* return the exact available payload size for the next packet for
00530    stream 'stream_index'. 'pts' and 'dts' are only used to know if
00531    timestamps are needed in the packet header. */
00532 static int get_packet_payload_size(AVFormatContext *ctx, int stream_index,
00533                                    int64_t pts, int64_t dts)
00534 {
00535     MpegMuxContext *s = ctx->priv_data;
00536     int buf_index;
00537     StreamInfo *stream;
00538 
00539     stream = ctx->streams[stream_index]->priv_data;
00540 
00541     buf_index = 0;
00542     if (((s->packet_number % s->pack_header_freq) == 0)) {
00543         /* pack header size */
00544         if (s->is_mpeg2)
00545             buf_index += 14;
00546         else
00547             buf_index += 12;
00548 
00549         if (s->is_vcd) {
00550             /* there is exactly one system header for each stream in a VCD MPEG,
00551                One in the very first video packet and one in the very first
00552                audio packet (see VCD standard p. IV-7 and IV-8).*/
00553 
00554             if (stream->packet_number==0)
00555                 /* The system headers refer only to the stream they occur in,
00556                    so they have a constant size.*/
00557                 buf_index += 15;
00558 
00559         } else {
00560             if ((s->packet_number % s->system_header_freq) == 0)
00561                 buf_index += s->system_header_size;
00562         }
00563     }
00564 
00565     if ((s->is_vcd && stream->packet_number==0)
00566         || (s->is_svcd && s->packet_number==0))
00567         /* the first pack of each stream contains only the pack header,
00568            the system header and some padding (see VCD standard p. IV-6)
00569            Add the padding size, so that the actual payload becomes 0.*/
00570         buf_index += s->packet_size - buf_index;
00571     else {
00572         /* packet header size */
00573         buf_index += 6;
00574         if (s->is_mpeg2) {
00575             buf_index += 3;
00576             if (stream->packet_number==0)
00577                 buf_index += 3; /* PES extension */
00578             buf_index += 1;    /* obligatory stuffing byte */
00579         }
00580         if (pts != AV_NOPTS_VALUE) {
00581             if (dts != pts)
00582                 buf_index += 5 + 5;
00583             else
00584                 buf_index += 5;
00585 
00586         } else {
00587             if (!s->is_mpeg2)
00588                 buf_index++;
00589         }
00590 
00591         if (stream->id < 0xc0) {
00592             /* AC-3/LPCM private data header */
00593             buf_index += 4;
00594             if (stream->id >= 0xa0) {
00595                 int n;
00596                 buf_index += 3;
00597                 /* NOTE: we round the payload size to an integer number of
00598                    LPCM samples */
00599                 n = (s->packet_size - buf_index) % stream->lpcm_align;
00600                 if (n)
00601                     buf_index += (stream->lpcm_align - n);
00602             }
00603         }
00604 
00605         if (s->is_vcd && (stream->id & 0xe0) == AUDIO_ID)
00606             /* The VCD standard demands that 20 zero bytes follow
00607                each audio packet (see standard p. IV-8).*/
00608             buf_index+=20;
00609     }
00610     return s->packet_size - buf_index;
00611 }
00612 #endif
00613 
00614 /* Write an MPEG padding packet header. */
00615 static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb,int packet_bytes)
00616 {
00617     MpegMuxContext *s = ctx->priv_data;
00618     int i;
00619 
00620     avio_wb32(pb, PADDING_STREAM);
00621     avio_wb16(pb, packet_bytes - 6);
00622     if (!s->is_mpeg2) {
00623         avio_w8(pb, 0x0f);
00624         packet_bytes -= 7;
00625     } else
00626         packet_bytes -= 6;
00627 
00628     for(i=0;i<packet_bytes;i++)
00629         avio_w8(pb, 0xff);
00630 }
00631 
00632 static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len){
00633     int nb_frames=0;
00634     PacketDesc *pkt_desc= stream->premux_packet;
00635 
00636     while(len>0){
00637         if(pkt_desc->size == pkt_desc->unwritten_size)
00638             nb_frames++;
00639         len -= pkt_desc->unwritten_size;
00640         pkt_desc= pkt_desc->next;
00641     }
00642 
00643     return nb_frames;
00644 }
00645 
00646 /* flush the packet on stream stream_index */
00647 static int flush_packet(AVFormatContext *ctx, int stream_index,
00648                          int64_t pts, int64_t dts, int64_t scr, int trailer_size)
00649 {
00650     MpegMuxContext *s = ctx->priv_data;
00651     StreamInfo *stream = ctx->streams[stream_index]->priv_data;
00652     uint8_t *buf_ptr;
00653     int size, payload_size, startcode, id, stuffing_size, i, header_len;
00654     int packet_size;
00655     uint8_t buffer[128];
00656     int zero_trail_bytes = 0;
00657     int pad_packet_bytes = 0;
00658     int pes_flags;
00659     int general_pack = 0;  /*"general" pack without data specific to one stream?*/
00660     int nb_frames;
00661 
00662     id = stream->id;
00663 
00664 #if 0
00665     printf("packet ID=%2x PTS=%0.3f\n",
00666            id, pts / 90000.0);
00667 #endif
00668 
00669     buf_ptr = buffer;
00670 
00671     if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) {
00672         /* output pack and systems header if needed */
00673         size = put_pack_header(ctx, buf_ptr, scr);
00674         buf_ptr += size;
00675         s->last_scr= scr;
00676 
00677         if (s->is_vcd) {
00678             /* there is exactly one system header for each stream in a VCD MPEG,
00679                One in the very first video packet and one in the very first
00680                audio packet (see VCD standard p. IV-7 and IV-8).*/
00681 
00682             if (stream->packet_number==0) {
00683                 size = put_system_header(ctx, buf_ptr, id);
00684                 buf_ptr += size;
00685             }
00686         } else if (s->is_dvd) {
00687             if (stream->align_iframe || s->packet_number == 0){
00688                 int PES_bytes_to_fill = s->packet_size - size - 10;
00689 
00690                 if (pts != AV_NOPTS_VALUE) {
00691                     if (dts != pts)
00692                         PES_bytes_to_fill -= 5 + 5;
00693                     else
00694                         PES_bytes_to_fill -= 5;
00695                 }
00696 
00697                 if (stream->bytes_to_iframe == 0 || s->packet_number == 0) {
00698                     size = put_system_header(ctx, buf_ptr, 0);
00699                     buf_ptr += size;
00700                     size = buf_ptr - buffer;
00701                     avio_write(ctx->pb, buffer, size);
00702 
00703                     avio_wb32(ctx->pb, PRIVATE_STREAM_2);
00704                     avio_wb16(ctx->pb, 0x03d4);         // length
00705                     avio_w8(ctx->pb, 0x00);           // substream ID, 00=PCI
00706                     for (i = 0; i < 979; i++)
00707                         avio_w8(ctx->pb, 0x00);
00708 
00709                     avio_wb32(ctx->pb, PRIVATE_STREAM_2);
00710                     avio_wb16(ctx->pb, 0x03fa);         // length
00711                     avio_w8(ctx->pb, 0x01);           // substream ID, 01=DSI
00712                     for (i = 0; i < 1017; i++)
00713                         avio_w8(ctx->pb, 0x00);
00714 
00715                     memset(buffer, 0, 128);
00716                     buf_ptr = buffer;
00717                     s->packet_number++;
00718                     stream->align_iframe = 0;
00719                     scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
00720                     size = put_pack_header(ctx, buf_ptr, scr);
00721                     s->last_scr= scr;
00722                     buf_ptr += size;
00723                     /* GOP Start */
00724                 } else if (stream->bytes_to_iframe < PES_bytes_to_fill) {
00725                     pad_packet_bytes = PES_bytes_to_fill - stream->bytes_to_iframe;
00726                 }
00727             }
00728         } else {
00729             if ((s->packet_number % s->system_header_freq) == 0) {
00730                 size = put_system_header(ctx, buf_ptr, 0);
00731                 buf_ptr += size;
00732             }
00733         }
00734     }
00735     size = buf_ptr - buffer;
00736     avio_write(ctx->pb, buffer, size);
00737 
00738     packet_size = s->packet_size - size;
00739 
00740     if (s->is_vcd && (id & 0xe0) == AUDIO_ID)
00741         /* The VCD standard demands that 20 zero bytes follow
00742            each audio pack (see standard p. IV-8).*/
00743         zero_trail_bytes += 20;
00744 
00745     if ((s->is_vcd && stream->packet_number==0)
00746         || (s->is_svcd && s->packet_number==0)) {
00747         /* for VCD the first pack of each stream contains only the pack header,
00748            the system header and lots of padding (see VCD standard p. IV-6).
00749            In the case of an audio pack, 20 zero bytes are also added at
00750            the end.*/
00751         /* For SVCD we fill the very first pack to increase compatibility with
00752            some DVD players. Not mandated by the standard.*/
00753         if (s->is_svcd)
00754             general_pack = 1;    /* the system header refers to both streams and no stream data*/
00755         pad_packet_bytes = packet_size - zero_trail_bytes;
00756     }
00757 
00758     packet_size -= pad_packet_bytes + zero_trail_bytes;
00759 
00760     if (packet_size > 0) {
00761 
00762         /* packet header size */
00763         packet_size -= 6;
00764 
00765         /* packet header */
00766         if (s->is_mpeg2) {
00767             header_len = 3;
00768             if (stream->packet_number==0)
00769                 header_len += 3; /* PES extension */
00770             header_len += 1; /* obligatory stuffing byte */
00771         } else {
00772             header_len = 0;
00773         }
00774         if (pts != AV_NOPTS_VALUE) {
00775             if (dts != pts)
00776                 header_len += 5 + 5;
00777             else
00778                 header_len += 5;
00779         } else {
00780             if (!s->is_mpeg2)
00781                 header_len++;
00782         }
00783 
00784         payload_size = packet_size - header_len;
00785         if (id < 0xc0) {
00786             startcode = PRIVATE_STREAM_1;
00787             payload_size -= 1;
00788             if (id >= 0x40) {
00789                 payload_size -= 3;
00790                 if (id >= 0xa0)
00791                     payload_size -= 3;
00792             }
00793         } else {
00794             startcode = 0x100 + id;
00795         }
00796 
00797         stuffing_size = payload_size - av_fifo_size(stream->fifo);
00798 
00799         // first byte does not fit -> reset pts/dts + stuffing
00800         if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){
00801             int timestamp_len=0;
00802             if(dts != pts)
00803                 timestamp_len += 5;
00804             if(pts != AV_NOPTS_VALUE)
00805                 timestamp_len += s->is_mpeg2 ? 5 : 4;
00806             pts=dts= AV_NOPTS_VALUE;
00807             header_len -= timestamp_len;
00808             if (s->is_dvd && stream->align_iframe) {
00809                 pad_packet_bytes += timestamp_len;
00810                 packet_size  -= timestamp_len;
00811             } else {
00812                 payload_size += timestamp_len;
00813             }
00814             stuffing_size += timestamp_len;
00815             if(payload_size > trailer_size)
00816                 stuffing_size += payload_size - trailer_size;
00817         }
00818 
00819         if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) { // can't use padding, so use stuffing
00820             packet_size += pad_packet_bytes;
00821             payload_size += pad_packet_bytes; // undo the previous adjustment
00822             if (stuffing_size < 0) {
00823                 stuffing_size  = pad_packet_bytes;
00824             } else {
00825                 stuffing_size += pad_packet_bytes;
00826             }
00827             pad_packet_bytes = 0;
00828         }
00829 
00830         if (stuffing_size < 0)
00831             stuffing_size = 0;
00832         if (stuffing_size > 16) {    /*<=16 for MPEG-1, <=32 for MPEG-2*/
00833             pad_packet_bytes += stuffing_size;
00834             packet_size      -= stuffing_size;
00835             payload_size     -= stuffing_size;
00836             stuffing_size = 0;
00837         }
00838 
00839         nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size);
00840 
00841         avio_wb32(ctx->pb, startcode);
00842 
00843         avio_wb16(ctx->pb, packet_size);
00844 
00845         if (!s->is_mpeg2)
00846             for(i=0;i<stuffing_size;i++)
00847                 avio_w8(ctx->pb, 0xff);
00848 
00849         if (s->is_mpeg2) {
00850             avio_w8(ctx->pb, 0x80); /* mpeg2 id */
00851 
00852             pes_flags=0;
00853 
00854             if (pts != AV_NOPTS_VALUE) {
00855                 pes_flags |= 0x80;
00856                 if (dts != pts)
00857                     pes_flags |= 0x40;
00858             }
00859 
00860             /* Both the MPEG-2 and the SVCD standards demand that the
00861                P-STD_buffer_size field be included in the first packet of
00862                every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
00863                and MPEG-2 standard 2.7.7) */
00864             if (stream->packet_number == 0)
00865                 pes_flags |= 0x01;
00866 
00867             avio_w8(ctx->pb, pes_flags); /* flags */
00868             avio_w8(ctx->pb, header_len - 3 + stuffing_size);
00869 
00870             if (pes_flags & 0x80)  /*write pts*/
00871                 put_timestamp(ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
00872             if (pes_flags & 0x40)  /*write dts*/
00873                 put_timestamp(ctx->pb, 0x01, dts);
00874 
00875             if (pes_flags & 0x01) {  /*write pes extension*/
00876                 avio_w8(ctx->pb, 0x10); /* flags */
00877 
00878                 /* P-STD buffer info */
00879                 if ((id & 0xe0) == AUDIO_ID)
00880                     avio_wb16(ctx->pb, 0x4000 | stream->max_buffer_size/ 128);
00881                 else
00882                     avio_wb16(ctx->pb, 0x6000 | stream->max_buffer_size/1024);
00883             }
00884 
00885         } else {
00886             if (pts != AV_NOPTS_VALUE) {
00887                 if (dts != pts) {
00888                     put_timestamp(ctx->pb, 0x03, pts);
00889                     put_timestamp(ctx->pb, 0x01, dts);
00890                 } else {
00891                     put_timestamp(ctx->pb, 0x02, pts);
00892                 }
00893             } else {
00894                 avio_w8(ctx->pb, 0x0f);
00895             }
00896         }
00897 
00898         if (s->is_mpeg2) {
00899             /* special stuffing byte that is always written
00900                to prevent accidental generation of start codes. */
00901             avio_w8(ctx->pb, 0xff);
00902 
00903             for(i=0;i<stuffing_size;i++)
00904                 avio_w8(ctx->pb, 0xff);
00905         }
00906 
00907         if (startcode == PRIVATE_STREAM_1) {
00908             avio_w8(ctx->pb, id);
00909             if (id >= 0xa0) {
00910                 /* LPCM (XXX: check nb_frames) */
00911                 avio_w8(ctx->pb, 7);
00912                 avio_wb16(ctx->pb, 4); /* skip 3 header bytes */
00913                 avio_w8(ctx->pb, stream->lpcm_header[0]);
00914                 avio_w8(ctx->pb, stream->lpcm_header[1]);
00915                 avio_w8(ctx->pb, stream->lpcm_header[2]);
00916             } else if (id >= 0x40) {
00917                 /* AC-3 */
00918                 avio_w8(ctx->pb, nb_frames);
00919                 avio_wb16(ctx->pb, trailer_size+1);
00920             }
00921         }
00922 
00923         /* output data */
00924         assert(payload_size - stuffing_size <= av_fifo_size(stream->fifo));
00925         av_fifo_generic_read(stream->fifo, ctx->pb, payload_size - stuffing_size, (void*)avio_write);
00926         stream->bytes_to_iframe -= payload_size - stuffing_size;
00927     }else{
00928         payload_size=
00929         stuffing_size= 0;
00930     }
00931 
00932     if (pad_packet_bytes > 0)
00933         put_padding_packet(ctx,ctx->pb, pad_packet_bytes);
00934 
00935     for(i=0;i<zero_trail_bytes;i++)
00936         avio_w8(ctx->pb, 0x00);
00937 
00938     avio_flush(ctx->pb);
00939 
00940     s->packet_number++;
00941 
00942     /* only increase the stream packet number if this pack actually contains
00943        something that is specific to this stream! I.e. a dedicated header
00944        or some data.*/
00945     if (!general_pack)
00946         stream->packet_number++;
00947 
00948     return payload_size - stuffing_size;
00949 }
00950 
00951 static void put_vcd_padding_sector(AVFormatContext *ctx)
00952 {
00953     /* There are two ways to do this padding: writing a sector/pack
00954        of 0 values, or writing an MPEG padding pack. Both seem to
00955        work with most decoders, BUT the VCD standard only allows a 0-sector
00956        (see standard p. IV-4, IV-5).
00957        So a 0-sector it is...*/
00958 
00959     MpegMuxContext *s = ctx->priv_data;
00960     int i;
00961 
00962     for(i=0;i<s->packet_size;i++)
00963         avio_w8(ctx->pb, 0);
00964 
00965     s->vcd_padding_bytes_written += s->packet_size;
00966 
00967     avio_flush(ctx->pb);
00968 
00969     /* increasing the packet number is correct. The SCR of the following packs
00970        is calculated from the packet_number and it has to include the padding
00971        sector (it represents the sector index, not the MPEG pack index)
00972        (see VCD standard p. IV-6)*/
00973     s->packet_number++;
00974 }
00975 
00976 #if 0 /* unused, remove? */
00977 static int64_t get_vcd_scr(AVFormatContext *ctx,int stream_index,int64_t pts)
00978 {
00979     MpegMuxContext *s = ctx->priv_data;
00980     int64_t scr;
00981 
00982         /* Since the data delivery rate is constant, SCR is computed
00983            using the formula C + i * 1200 where C is the start constant
00984            and i is the pack index.
00985            It is recommended that SCR 0 is at the beginning of the VCD front
00986            margin (a sequence of empty Form 2 sectors on the CD).
00987            It is recommended that the front margin is 30 sectors long, so
00988            we use C = 30*1200 = 36000
00989            (Note that even if the front margin is not 30 sectors the file
00990            will still be correct according to the standard. It just won't have
00991            the "recommended" value).*/
00992         scr = 36000 + s->packet_number * 1200;
00993 
00994     return scr;
00995 }
00996 #endif
00997 
00998 static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){
00999 //    MpegMuxContext *s = ctx->priv_data;
01000     int i;
01001 
01002     for(i=0; i<ctx->nb_streams; i++){
01003         AVStream *st = ctx->streams[i];
01004         StreamInfo *stream = st->priv_data;
01005         PacketDesc *pkt_desc;
01006 
01007         while((pkt_desc= stream->predecode_packet)
01008               && scr > pkt_desc->dts){ //FIXME > vs >=
01009             if(stream->buffer_index < pkt_desc->size ||
01010                stream->predecode_packet == stream->premux_packet){
01011                 av_log(ctx, AV_LOG_ERROR,
01012                        "buffer underflow i=%d bufi=%d size=%d\n",
01013                        i, stream->buffer_index, pkt_desc->size);
01014                 break;
01015             }
01016             stream->buffer_index -= pkt_desc->size;
01017 
01018             stream->predecode_packet= pkt_desc->next;
01019             av_freep(&pkt_desc);
01020         }
01021     }
01022 
01023     return 0;
01024 }
01025 
01026 static int output_packet(AVFormatContext *ctx, int flush){
01027     MpegMuxContext *s = ctx->priv_data;
01028     AVStream *st;
01029     StreamInfo *stream;
01030     int i, avail_space=0, es_size, trailer_size;
01031     int best_i= -1;
01032     int best_score= INT_MIN;
01033     int ignore_constraints=0;
01034     int64_t scr= s->last_scr;
01035     PacketDesc *timestamp_packet;
01036     const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
01037 
01038 retry:
01039     for(i=0; i<ctx->nb_streams; i++){
01040         AVStream *st = ctx->streams[i];
01041         StreamInfo *stream = st->priv_data;
01042         const int avail_data=  av_fifo_size(stream->fifo);
01043         const int space= stream->max_buffer_size - stream->buffer_index;
01044         int rel_space= 1024*space / stream->max_buffer_size;
01045         PacketDesc *next_pkt= stream->premux_packet;
01046 
01047         /* for subtitle, a single PES packet must be generated,
01048            so we flush after every single subtitle packet */
01049         if(s->packet_size > avail_data && !flush
01050            && st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE)
01051             return 0;
01052         if(avail_data==0)
01053             continue;
01054         assert(avail_data>0);
01055 
01056         if(space < s->packet_size && !ignore_constraints)
01057             continue;
01058 
01059         if(next_pkt && next_pkt->dts - scr > max_delay)
01060             continue;
01061 
01062         if(rel_space > best_score){
01063             best_score= rel_space;
01064             best_i = i;
01065             avail_space= space;
01066         }
01067     }
01068 
01069     if(best_i < 0){
01070         int64_t best_dts= INT64_MAX;
01071 
01072         for(i=0; i<ctx->nb_streams; i++){
01073             AVStream *st = ctx->streams[i];
01074             StreamInfo *stream = st->priv_data;
01075             PacketDesc *pkt_desc= stream->predecode_packet;
01076             if(pkt_desc && pkt_desc->dts < best_dts)
01077                 best_dts= pkt_desc->dts;
01078         }
01079 
01080         av_dlog(ctx, "bumping scr, scr:%f, dts:%f\n",
01081                 scr / 90000.0, best_dts / 90000.0);
01082         if(best_dts == INT64_MAX)
01083             return 0;
01084 
01085         if(scr >= best_dts+1 && !ignore_constraints){
01086             av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n");
01087             ignore_constraints= 1;
01088         }
01089         scr= FFMAX(best_dts+1, scr);
01090         if(remove_decoded_packets(ctx, scr) < 0)
01091             return -1;
01092         goto retry;
01093     }
01094 
01095     assert(best_i >= 0);
01096 
01097     st = ctx->streams[best_i];
01098     stream = st->priv_data;
01099 
01100     assert(av_fifo_size(stream->fifo) > 0);
01101 
01102     assert(avail_space >= s->packet_size || ignore_constraints);
01103 
01104     timestamp_packet= stream->premux_packet;
01105     if(timestamp_packet->unwritten_size == timestamp_packet->size){
01106         trailer_size= 0;
01107     }else{
01108         trailer_size= timestamp_packet->unwritten_size;
01109         timestamp_packet= timestamp_packet->next;
01110     }
01111 
01112     if(timestamp_packet){
01113 //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);
01114         es_size= flush_packet(ctx, best_i, timestamp_packet->pts, timestamp_packet->dts, scr, trailer_size);
01115     }else{
01116         assert(av_fifo_size(stream->fifo) == trailer_size);
01117         es_size= flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr, trailer_size);
01118     }
01119 
01120     if (s->is_vcd) {
01121         /* Write one or more padding sectors, if necessary, to reach
01122            the constant overall bitrate.*/
01123         int vcd_pad_bytes;
01124 
01125         while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->premux_packet->pts) ) >= s->packet_size){ //FIXME pts cannot be correct here
01126             put_vcd_padding_sector(ctx);
01127             s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
01128         }
01129     }
01130 
01131     stream->buffer_index += es_size;
01132     s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
01133 
01134     while(stream->premux_packet && stream->premux_packet->unwritten_size <= es_size){
01135         es_size -= stream->premux_packet->unwritten_size;
01136         stream->premux_packet= stream->premux_packet->next;
01137     }
01138     if(es_size)
01139         stream->premux_packet->unwritten_size -= es_size;
01140 
01141     if(remove_decoded_packets(ctx, s->last_scr) < 0)
01142         return -1;
01143 
01144     return 1;
01145 }
01146 
01147 static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
01148 {
01149     MpegMuxContext *s = ctx->priv_data;
01150     int stream_index= pkt->stream_index;
01151     int size= pkt->size;
01152     uint8_t *buf= pkt->data;
01153     AVStream *st = ctx->streams[stream_index];
01154     StreamInfo *stream = st->priv_data;
01155     int64_t pts, dts;
01156     PacketDesc *pkt_desc;
01157     const int preload= av_rescale(ctx->preload, 90000, AV_TIME_BASE);
01158     const int is_iframe = st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (pkt->flags & AV_PKT_FLAG_KEY);
01159 
01160     pts= pkt->pts;
01161     dts= pkt->dts;
01162 
01163     if(pts != AV_NOPTS_VALUE) pts += 2*preload;
01164     if(dts != AV_NOPTS_VALUE){
01165         if(!s->last_scr)
01166             s->last_scr= dts + preload;
01167         dts += 2*preload;
01168     }
01169 
01170 //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);
01171     if (!stream->premux_packet)
01172         stream->next_packet = &stream->premux_packet;
01173     *stream->next_packet=
01174     pkt_desc= av_mallocz(sizeof(PacketDesc));
01175     pkt_desc->pts= pts;
01176     pkt_desc->dts= dts;
01177     pkt_desc->unwritten_size=
01178     pkt_desc->size= size;
01179     if(!stream->predecode_packet)
01180         stream->predecode_packet= pkt_desc;
01181     stream->next_packet= &pkt_desc->next;
01182 
01183     if (av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size) < 0)
01184         return -1;
01185 
01186     if (s->is_dvd){
01187         if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) { // min VOBU length 0.4 seconds (mpucoder)
01188             stream->bytes_to_iframe = av_fifo_size(stream->fifo);
01189             stream->align_iframe = 1;
01190             stream->vobu_start_pts = pts;
01191         }
01192     }
01193 
01194     av_fifo_generic_write(stream->fifo, buf, size, NULL);
01195 
01196     for(;;){
01197         int ret= output_packet(ctx, 0);
01198         if(ret<=0)
01199             return ret;
01200     }
01201 }
01202 
01203 static int mpeg_mux_end(AVFormatContext *ctx)
01204 {
01205 //    MpegMuxContext *s = ctx->priv_data;
01206     StreamInfo *stream;
01207     int i;
01208 
01209     for(;;){
01210         int ret= output_packet(ctx, 1);
01211         if(ret<0)
01212             return ret;
01213         else if(ret==0)
01214             break;
01215     }
01216 
01217     /* End header according to MPEG1 systems standard. We do not write
01218        it as it is usually not needed by decoders and because it
01219        complicates MPEG stream concatenation. */
01220     //avio_wb32(ctx->pb, ISO_11172_END_CODE);
01221     //avio_flush(ctx->pb);
01222 
01223     for(i=0;i<ctx->nb_streams;i++) {
01224         stream = ctx->streams[i]->priv_data;
01225 
01226         assert(av_fifo_size(stream->fifo) == 0);
01227         av_fifo_free(stream->fifo);
01228     }
01229     return 0;
01230 }
01231 
01232 #if CONFIG_MPEG1SYSTEM_MUXER
01233 AVOutputFormat ff_mpeg1system_muxer = {
01234     "mpeg",
01235     NULL_IF_CONFIG_SMALL("MPEG-1 System format"),
01236     "video/mpeg",
01237     "mpg,mpeg",
01238     sizeof(MpegMuxContext),
01239     CODEC_ID_MP2,
01240     CODEC_ID_MPEG1VIDEO,
01241     mpeg_mux_init,
01242     mpeg_mux_write_packet,
01243     mpeg_mux_end,
01244 };
01245 #endif
01246 #if CONFIG_MPEG1VCD_MUXER
01247 AVOutputFormat ff_mpeg1vcd_muxer = {
01248     "vcd",
01249     NULL_IF_CONFIG_SMALL("MPEG-1 System format (VCD)"),
01250     "video/mpeg",
01251     NULL,
01252     sizeof(MpegMuxContext),
01253     CODEC_ID_MP2,
01254     CODEC_ID_MPEG1VIDEO,
01255     mpeg_mux_init,
01256     mpeg_mux_write_packet,
01257     mpeg_mux_end,
01258 };
01259 #endif
01260 #if CONFIG_MPEG2VOB_MUXER
01261 AVOutputFormat ff_mpeg2vob_muxer = {
01262     "vob",
01263     NULL_IF_CONFIG_SMALL("MPEG-2 PS format (VOB)"),
01264     "video/mpeg",
01265     "vob",
01266     sizeof(MpegMuxContext),
01267     CODEC_ID_MP2,
01268     CODEC_ID_MPEG2VIDEO,
01269     mpeg_mux_init,
01270     mpeg_mux_write_packet,
01271     mpeg_mux_end,
01272 };
01273 #endif
01274 
01275 /* Same as mpeg2vob_mux except that the pack size is 2324 */
01276 #if CONFIG_MPEG2SVCD_MUXER
01277 AVOutputFormat ff_mpeg2svcd_muxer = {
01278     "svcd",
01279     NULL_IF_CONFIG_SMALL("MPEG-2 PS format (VOB)"),
01280     "video/mpeg",
01281     "vob",
01282     sizeof(MpegMuxContext),
01283     CODEC_ID_MP2,
01284     CODEC_ID_MPEG2VIDEO,
01285     mpeg_mux_init,
01286     mpeg_mux_write_packet,
01287     mpeg_mux_end,
01288 };
01289 #endif
01290 
01291 /*  Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
01292 #if CONFIG_MPEG2DVD_MUXER
01293 AVOutputFormat ff_mpeg2dvd_muxer = {
01294     "dvd",
01295     NULL_IF_CONFIG_SMALL("MPEG-2 PS format (DVD VOB)"),
01296     "video/mpeg",
01297     "dvd",
01298     sizeof(MpegMuxContext),
01299     CODEC_ID_MP2,
01300     CODEC_ID_MPEG2VIDEO,
01301     mpeg_mux_init,
01302     mpeg_mux_write_packet,
01303     mpeg_mux_end,
01304 };
01305 #endif

Generated on Fri Feb 22 2013 07:24:31 for FFmpeg by  doxygen 1.7.1