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

Generated on Fri Sep 16 2011 17:17:49 for FFmpeg by  doxygen 1.7.1