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

Generated on Tue Nov 4 2014 12:59:23 for ffmpeg by  doxygen 1.7.1