]> git.sesse.net Git - ffmpeg/blob - libavformat/mpegenc.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / mpegenc.c
1 /*
2  * MPEG1/2 muxer
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/fifo.h"
23 #include "libavutil/log.h"
24 #include "libavutil/mathematics.h"
25 #include "libavutil/opt.h"
26 #include "libavcodec/put_bits.h"
27 #include "avformat.h"
28 #include "internal.h"
29 #include "mpeg.h"
30
31 #define MAX_PAYLOAD_SIZE 4096
32
33 #undef NDEBUG
34 #include <assert.h>
35
36 typedef struct PacketDesc {
37     int64_t pts;
38     int64_t dts;
39     int size;
40     int unwritten_size;
41     int flags;
42     struct PacketDesc *next;
43 } PacketDesc;
44
45 typedef struct {
46     AVFifoBuffer *fifo;
47     uint8_t id;
48     int max_buffer_size; /* in bytes */
49     int buffer_index;
50     PacketDesc *predecode_packet;
51     PacketDesc *premux_packet;
52     PacketDesc **next_packet;
53     int packet_number;
54     uint8_t lpcm_header[3];
55     int lpcm_align;
56     int bytes_to_iframe;
57     int align_iframe;
58     int64_t vobu_start_pts;
59 } StreamInfo;
60
61 typedef struct {
62     const AVClass *class;
63     int packet_size; /* required packet size */
64     int packet_number;
65     int pack_header_freq;     /* frequency (in packets^-1) at which we send pack headers */
66     int system_header_freq;
67     int system_header_size;
68     int user_mux_rate; /* bitrate in units of bits/s */
69     int mux_rate; /* bitrate in units of 50 bytes/s */
70     /* stream info */
71     int audio_bound;
72     int video_bound;
73     int is_mpeg2;
74     int is_vcd;
75     int is_svcd;
76     int is_dvd;
77     int64_t last_scr; /* current system clock */
78
79     double vcd_padding_bitrate; //FIXME floats
80     int64_t vcd_padding_bytes_written;
81
82     int preload;
83 } MpegMuxContext;
84
85 extern AVOutputFormat ff_mpeg1vcd_muxer;
86 extern AVOutputFormat ff_mpeg2dvd_muxer;
87 extern AVOutputFormat ff_mpeg2svcd_muxer;
88 extern AVOutputFormat ff_mpeg2vob_muxer;
89
90 static int put_pack_header(AVFormatContext *ctx,
91                            uint8_t *buf, int64_t timestamp)
92 {
93     MpegMuxContext *s = ctx->priv_data;
94     PutBitContext pb;
95
96     init_put_bits(&pb, buf, 128);
97
98     put_bits32(&pb, PACK_START_CODE);
99     if (s->is_mpeg2) {
100         put_bits(&pb, 2, 0x1);
101     } else {
102         put_bits(&pb, 4, 0x2);
103     }
104     put_bits(&pb, 3,  (uint32_t)((timestamp >> 30) & 0x07));
105     put_bits(&pb, 1, 1);
106     put_bits(&pb, 15, (uint32_t)((timestamp >> 15) & 0x7fff));
107     put_bits(&pb, 1, 1);
108     put_bits(&pb, 15, (uint32_t)((timestamp      ) & 0x7fff));
109     put_bits(&pb, 1, 1);
110     if (s->is_mpeg2) {
111         /* clock extension */
112         put_bits(&pb, 9, 0);
113     }
114     put_bits(&pb, 1, 1);
115     put_bits(&pb, 22, s->mux_rate);
116     put_bits(&pb, 1, 1);
117     if (s->is_mpeg2) {
118         put_bits(&pb, 1, 1);
119         put_bits(&pb, 5, 0x1f); /* reserved */
120         put_bits(&pb, 3, 0); /* stuffing length */
121     }
122     flush_put_bits(&pb);
123     return put_bits_ptr(&pb) - pb.buf;
124 }
125
126 static int put_system_header(AVFormatContext *ctx, uint8_t *buf,int only_for_stream_id)
127 {
128     MpegMuxContext *s = ctx->priv_data;
129     int size, i, private_stream_coded, id;
130     PutBitContext pb;
131
132     init_put_bits(&pb, buf, 128);
133
134     put_bits32(&pb, SYSTEM_HEADER_START_CODE);
135     put_bits(&pb, 16, 0);
136     put_bits(&pb, 1, 1);
137
138     put_bits(&pb, 22, s->mux_rate); /* maximum bit rate of the multiplexed stream */
139     put_bits(&pb, 1, 1); /* marker */
140     if (s->is_vcd && only_for_stream_id==VIDEO_ID) {
141         /* This header applies only to the video stream (see VCD standard p. IV-7)*/
142         put_bits(&pb, 6, 0);
143     } else
144         put_bits(&pb, 6, s->audio_bound);
145
146     if (s->is_vcd) {
147         /* see VCD standard, p. IV-7*/
148         put_bits(&pb, 1, 0);
149         put_bits(&pb, 1, 1);
150     } else {
151         put_bits(&pb, 1, 0); /* variable bitrate*/
152         put_bits(&pb, 1, 0); /* non constrainted bit stream */
153     }
154
155     if (s->is_vcd || s->is_dvd) {
156         /* see VCD standard p IV-7 */
157         put_bits(&pb, 1, 1); /* audio locked */
158         put_bits(&pb, 1, 1); /* video locked */
159     } else {
160         put_bits(&pb, 1, 0); /* audio locked */
161         put_bits(&pb, 1, 0); /* video locked */
162     }
163
164     put_bits(&pb, 1, 1); /* marker */
165
166     if (s->is_vcd && (only_for_stream_id & 0xe0) == AUDIO_ID) {
167         /* This header applies only to the audio stream (see VCD standard p. IV-7)*/
168         put_bits(&pb, 5, 0);
169     } else
170         put_bits(&pb, 5, s->video_bound);
171
172     if (s->is_dvd) {
173         put_bits(&pb, 1, 0);    /* packet_rate_restriction_flag */
174         put_bits(&pb, 7, 0x7f); /* reserved byte */
175     } else
176         put_bits(&pb, 8, 0xff); /* reserved byte */
177
178     /* DVD-Video Stream_bound entries
179     id (0xB9) video, maximum P-STD for stream 0xE0. (P-STD_buffer_bound_scale = 1)
180     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)
181     id (0xBD) private stream 1 (audio other than MPEG and subpictures). (P-STD_buffer_bound_scale = 1)
182     id (0xBF) private stream 2, NAV packs, set to 2x1024. */
183     if (s->is_dvd) {
184
185         int P_STD_max_video = 0;
186         int P_STD_max_mpeg_audio = 0;
187         int P_STD_max_mpeg_PS1 = 0;
188
189         for(i=0;i<ctx->nb_streams;i++) {
190             StreamInfo *stream = ctx->streams[i]->priv_data;
191
192             id = stream->id;
193             if (id == 0xbd && stream->max_buffer_size > P_STD_max_mpeg_PS1) {
194                 P_STD_max_mpeg_PS1 = stream->max_buffer_size;
195             } else if (id >= 0xc0 && id <= 0xc7 && stream->max_buffer_size > P_STD_max_mpeg_audio) {
196                 P_STD_max_mpeg_audio = stream->max_buffer_size;
197             } else if (id == 0xe0 && stream->max_buffer_size > P_STD_max_video) {
198                 P_STD_max_video = stream->max_buffer_size;
199             }
200         }
201
202         /* video */
203         put_bits(&pb, 8, 0xb9); /* stream ID */
204         put_bits(&pb, 2, 3);
205         put_bits(&pb, 1, 1);
206         put_bits(&pb, 13, P_STD_max_video / 1024);
207
208         /* audio */
209         if (P_STD_max_mpeg_audio == 0)
210             P_STD_max_mpeg_audio = 4096;
211         put_bits(&pb, 8, 0xb8); /* stream ID */
212         put_bits(&pb, 2, 3);
213         put_bits(&pb, 1, 0);
214         put_bits(&pb, 13, P_STD_max_mpeg_audio / 128);
215
216         /* private stream 1 */
217         put_bits(&pb, 8, 0xbd); /* stream ID */
218         put_bits(&pb, 2, 3);
219         put_bits(&pb, 1, 0);
220         put_bits(&pb, 13, P_STD_max_mpeg_PS1 / 128);
221
222         /* private stream 2 */
223         put_bits(&pb, 8, 0xbf); /* stream ID */
224         put_bits(&pb, 2, 3);
225         put_bits(&pb, 1, 1);
226         put_bits(&pb, 13, 2);
227     }
228     else {
229         /* audio stream info */
230         private_stream_coded = 0;
231         for(i=0;i<ctx->nb_streams;i++) {
232             StreamInfo *stream = ctx->streams[i]->priv_data;
233
234
235             /* For VCDs, only include the stream info for the stream
236             that the pack which contains this system belongs to.
237             (see VCD standard p. IV-7) */
238             if ( !s->is_vcd || stream->id==only_for_stream_id
239                 || only_for_stream_id==0) {
240
241                 id = stream->id;
242                 if (id < 0xc0) {
243                     /* special case for private streams (AC-3 uses that) */
244                     if (private_stream_coded)
245                         continue;
246                     private_stream_coded = 1;
247                     id = 0xbd;
248                 }
249                 put_bits(&pb, 8, id); /* stream ID */
250                 put_bits(&pb, 2, 3);
251                 if (id < 0xe0) {
252                     /* audio */
253                     put_bits(&pb, 1, 0);
254                     put_bits(&pb, 13, stream->max_buffer_size / 128);
255                 } else {
256                     /* video */
257                     put_bits(&pb, 1, 1);
258                     put_bits(&pb, 13, stream->max_buffer_size / 1024);
259                 }
260             }
261         }
262     }
263
264     flush_put_bits(&pb);
265     size = put_bits_ptr(&pb) - pb.buf;
266     /* patch packet size */
267     buf[4] = (size - 6) >> 8;
268     buf[5] = (size - 6) & 0xff;
269
270     return size;
271 }
272
273 static int get_system_header_size(AVFormatContext *ctx)
274 {
275     int buf_index, i, private_stream_coded;
276     StreamInfo *stream;
277     MpegMuxContext *s = ctx->priv_data;
278
279     if (s->is_dvd)
280        return 18; // DVD-Video system headers are 18 bytes fixed length.
281
282     buf_index = 12;
283     private_stream_coded = 0;
284     for(i=0;i<ctx->nb_streams;i++) {
285         stream = ctx->streams[i]->priv_data;
286         if (stream->id < 0xc0) {
287             if (private_stream_coded)
288                 continue;
289             private_stream_coded = 1;
290         }
291         buf_index += 3;
292     }
293     return buf_index;
294 }
295
296 static int mpeg_mux_init(AVFormatContext *ctx)
297 {
298     MpegMuxContext *s = ctx->priv_data;
299     int bitrate, i, mpa_id, mpv_id, mps_id, ac3_id, dts_id, lpcm_id, j;
300     AVStream *st;
301     StreamInfo *stream;
302     int audio_bitrate;
303     int video_bitrate;
304
305     s->packet_number = 0;
306     s->is_vcd =    (CONFIG_MPEG1VCD_MUXER  && ctx->oformat == &ff_mpeg1vcd_muxer);
307     s->is_svcd =   (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer);
308     s->is_mpeg2 = ((CONFIG_MPEG2VOB_MUXER  && ctx->oformat == &ff_mpeg2vob_muxer) ||
309                    (CONFIG_MPEG2DVD_MUXER  && ctx->oformat == &ff_mpeg2dvd_muxer) ||
310                    (CONFIG_MPEG2SVCD_MUXER && ctx->oformat == &ff_mpeg2svcd_muxer));
311     s->is_dvd =    (CONFIG_MPEG2DVD_MUXER  && ctx->oformat == &ff_mpeg2dvd_muxer);
312
313     if(ctx->packet_size) {
314         if (ctx->packet_size < 20 || ctx->packet_size > (1 << 23) + 10) {
315             av_log(ctx, AV_LOG_ERROR, "Invalid packet size %d\n",
316                    ctx->packet_size);
317             goto fail;
318         }
319         s->packet_size = ctx->packet_size;
320     } else
321         s->packet_size = 2048;
322     if (ctx->max_delay < 0) /* Not set by the caller */
323         ctx->max_delay = 0;
324
325     s->vcd_padding_bytes_written = 0;
326     s->vcd_padding_bitrate=0;
327
328     s->audio_bound = 0;
329     s->video_bound = 0;
330     mpa_id = AUDIO_ID;
331     ac3_id = AC3_ID;
332     dts_id = DTS_ID;
333     mpv_id = VIDEO_ID;
334     mps_id = SUB_ID;
335     lpcm_id = LPCM_ID;
336     for(i=0;i<ctx->nb_streams;i++) {
337         st = ctx->streams[i];
338         stream = av_mallocz(sizeof(StreamInfo));
339         if (!stream)
340             goto fail;
341         st->priv_data = stream;
342
343         avpriv_set_pts_info(st, 64, 1, 90000);
344
345         switch(st->codec->codec_type) {
346         case AVMEDIA_TYPE_AUDIO:
347             if        (st->codec->codec_id == CODEC_ID_AC3) {
348                 stream->id = ac3_id++;
349             } else if (st->codec->codec_id == CODEC_ID_DTS) {
350                 stream->id = dts_id++;
351             } else if (st->codec->codec_id == CODEC_ID_PCM_S16BE) {
352                 stream->id = lpcm_id++;
353                 for(j = 0; j < 4; j++) {
354                     if (lpcm_freq_tab[j] == st->codec->sample_rate)
355                         break;
356                 }
357                 if (j == 4)
358                     goto fail;
359                 if (st->codec->channels > 8)
360                     return -1;
361                 stream->lpcm_header[0] = 0x0c;
362                 stream->lpcm_header[1] = (st->codec->channels - 1) | (j << 4);
363                 stream->lpcm_header[2] = 0x80;
364                 stream->lpcm_align = st->codec->channels * 2;
365             } else {
366                 stream->id = mpa_id++;
367             }
368
369             /* This value HAS to be used for VCD (see VCD standard, p. IV-7).
370                Right now it is also used for everything else.*/
371             stream->max_buffer_size = 4 * 1024;
372             s->audio_bound++;
373             break;
374         case AVMEDIA_TYPE_VIDEO:
375             stream->id = mpv_id++;
376             if (st->codec->rc_buffer_size)
377                 stream->max_buffer_size = 6*1024 + st->codec->rc_buffer_size/8;
378             else {
379                 av_log(ctx, AV_LOG_WARNING, "VBV buffer size not set, muxing may fail\n");
380                 stream->max_buffer_size = 230*1024; //FIXME this is probably too small as default
381             }
382 #if 0
383                 /* see VCD standard, p. IV-7*/
384                 stream->max_buffer_size = 46 * 1024;
385             else
386                 /* This value HAS to be used for SVCD (see SVCD standard, p. 26 V.2.3.2).
387                    Right now it is also used for everything else.*/
388                 stream->max_buffer_size = 230 * 1024;
389 #endif
390             s->video_bound++;
391             break;
392         case AVMEDIA_TYPE_SUBTITLE:
393             stream->id = mps_id++;
394             stream->max_buffer_size = 16 * 1024;
395             break;
396         default:
397             return -1;
398         }
399         stream->fifo= av_fifo_alloc(16);
400         if (!stream->fifo)
401             goto fail;
402     }
403     bitrate = 0;
404     audio_bitrate = 0;
405     video_bitrate = 0;
406     for(i=0;i<ctx->nb_streams;i++) {
407         int codec_rate;
408         st = ctx->streams[i];
409         stream = (StreamInfo*) st->priv_data;
410
411         if(st->codec->rc_max_rate || stream->id==VIDEO_ID)
412             codec_rate= st->codec->rc_max_rate;
413         else
414             codec_rate= st->codec->bit_rate;
415
416         if(!codec_rate)
417             codec_rate= (1<<21)*8*50/ctx->nb_streams;
418
419         bitrate += codec_rate;
420
421         if ((stream->id & 0xe0) == AUDIO_ID)
422             audio_bitrate += codec_rate;
423         else if (stream->id==VIDEO_ID)
424             video_bitrate += codec_rate;
425     }
426
427     if (s->user_mux_rate) {
428         s->mux_rate = (s->user_mux_rate + (8 * 50) - 1) / (8 * 50);
429     } else {
430         /* we increase slightly the bitrate to take into account the
431            headers. XXX: compute it exactly */
432         bitrate += bitrate / 20;
433         bitrate += 10000;
434         s->mux_rate = (bitrate + (8 * 50) - 1) / (8 * 50);
435     }
436
437     if (s->is_vcd) {
438         double overhead_rate;
439
440         /* The VCD standard mandates that the mux_rate field is 3528
441            (see standard p. IV-6).
442            The value is actually "wrong", i.e. if you calculate
443            it using the normal formula and the 75 sectors per second transfer
444            rate you get a different value because the real pack size is 2324,
445            not 2352. But the standard explicitly specifies that the mux_rate
446            field in the header must have this value.*/
447 //        s->mux_rate=2352 * 75 / 50;    /* = 3528*/
448
449         /* The VCD standard states that the muxed stream must be
450            exactly 75 packs / second (the data rate of a single speed cdrom).
451            Since the video bitrate (probably 1150000 bits/sec) will be below
452            the theoretical maximum we have to add some padding packets
453            to make up for the lower data rate.
454            (cf. VCD standard p. IV-6 )*/
455
456         /* Add the header overhead to the data rate.
457            2279 data bytes per audio pack, 2294 data bytes per video pack*/
458         overhead_rate  = ((audio_bitrate / 8.0) / 2279) * (2324 - 2279);
459         overhead_rate += ((video_bitrate / 8.0) / 2294) * (2324 - 2294);
460         overhead_rate *= 8;
461
462         /* Add padding so that the full bitrate is 2324*75 bytes/sec */
463         s->vcd_padding_bitrate = 2324 * 75 * 8 - (bitrate + overhead_rate);
464     }
465
466     if (s->is_vcd || s->is_mpeg2)
467         /* every packet */
468         s->pack_header_freq = 1;
469     else
470         /* every 2 seconds */
471         s->pack_header_freq = 2 * bitrate / s->packet_size / 8;
472
473     /* the above seems to make pack_header_freq zero sometimes */
474     if (s->pack_header_freq == 0)
475        s->pack_header_freq = 1;
476
477     if (s->is_mpeg2)
478         /* every 200 packets. Need to look at the spec.  */
479         s->system_header_freq = s->pack_header_freq * 40;
480     else if (s->is_vcd)
481         /* the standard mandates that there are only two system headers
482            in the whole file: one in the first packet of each stream.
483            (see standard p. IV-7 and IV-8) */
484         s->system_header_freq = 0x7fffffff;
485     else
486         s->system_header_freq = s->pack_header_freq * 5;
487
488     for(i=0;i<ctx->nb_streams;i++) {
489         stream = ctx->streams[i]->priv_data;
490         stream->packet_number = 0;
491     }
492     s->system_header_size = get_system_header_size(ctx);
493     s->last_scr = 0;
494     return 0;
495  fail:
496     for(i=0;i<ctx->nb_streams;i++) {
497         av_free(ctx->streams[i]->priv_data);
498     }
499     return AVERROR(ENOMEM);
500 }
501
502 static inline void put_timestamp(AVIOContext *pb, int id, int64_t timestamp)
503 {
504     avio_w8(pb,
505              (id << 4) |
506              (((timestamp >> 30) & 0x07) << 1) |
507              1);
508     avio_wb16(pb, (uint16_t)((((timestamp >> 15) & 0x7fff) << 1) | 1));
509     avio_wb16(pb, (uint16_t)((((timestamp      ) & 0x7fff) << 1) | 1));
510 }
511
512
513 /* return the number of padding bytes that should be inserted into
514    the multiplexed stream.*/
515 static int get_vcd_padding_size(AVFormatContext *ctx, int64_t pts)
516 {
517     MpegMuxContext *s = ctx->priv_data;
518     int pad_bytes = 0;
519
520     if (s->vcd_padding_bitrate > 0 && pts!=AV_NOPTS_VALUE)
521     {
522         int64_t full_pad_bytes;
523
524         full_pad_bytes = (int64_t)((s->vcd_padding_bitrate * (pts / 90000.0)) / 8.0); //FIXME this is wrong
525         pad_bytes = (int) (full_pad_bytes - s->vcd_padding_bytes_written);
526
527         if (pad_bytes<0)
528             /* might happen if we have already padded to a later timestamp. This
529                can occur if another stream has already advanced further.*/
530             pad_bytes=0;
531     }
532
533     return pad_bytes;
534 }
535
536
537 #if 0 /* unused, remove? */
538 /* return the exact available payload size for the next packet for
539    stream 'stream_index'. 'pts' and 'dts' are only used to know if
540    timestamps are needed in the packet header. */
541 static int get_packet_payload_size(AVFormatContext *ctx, int stream_index,
542                                    int64_t pts, int64_t dts)
543 {
544     MpegMuxContext *s = ctx->priv_data;
545     int buf_index;
546     StreamInfo *stream;
547
548     stream = ctx->streams[stream_index]->priv_data;
549
550     buf_index = 0;
551     if (((s->packet_number % s->pack_header_freq) == 0)) {
552         /* pack header size */
553         if (s->is_mpeg2)
554             buf_index += 14;
555         else
556             buf_index += 12;
557
558         if (s->is_vcd) {
559             /* there is exactly one system header for each stream in a VCD MPEG,
560                One in the very first video packet and one in the very first
561                audio packet (see VCD standard p. IV-7 and IV-8).*/
562
563             if (stream->packet_number==0)
564                 /* The system headers refer only to the stream they occur in,
565                    so they have a constant size.*/
566                 buf_index += 15;
567
568         } else {
569             if ((s->packet_number % s->system_header_freq) == 0)
570                 buf_index += s->system_header_size;
571         }
572     }
573
574     if ((s->is_vcd && stream->packet_number==0)
575         || (s->is_svcd && s->packet_number==0))
576         /* the first pack of each stream contains only the pack header,
577            the system header and some padding (see VCD standard p. IV-6)
578            Add the padding size, so that the actual payload becomes 0.*/
579         buf_index += s->packet_size - buf_index;
580     else {
581         /* packet header size */
582         buf_index += 6;
583         if (s->is_mpeg2) {
584             buf_index += 3;
585             if (stream->packet_number==0)
586                 buf_index += 3; /* PES extension */
587             buf_index += 1;    /* obligatory stuffing byte */
588         }
589         if (pts != AV_NOPTS_VALUE) {
590             if (dts != pts)
591                 buf_index += 5 + 5;
592             else
593                 buf_index += 5;
594
595         } else {
596             if (!s->is_mpeg2)
597                 buf_index++;
598         }
599
600         if (stream->id < 0xc0) {
601             /* AC-3/LPCM private data header */
602             buf_index += 4;
603             if (stream->id >= 0xa0) {
604                 int n;
605                 buf_index += 3;
606                 /* NOTE: we round the payload size to an integer number of
607                    LPCM samples */
608                 n = (s->packet_size - buf_index) % stream->lpcm_align;
609                 if (n)
610                     buf_index += (stream->lpcm_align - n);
611             }
612         }
613
614         if (s->is_vcd && (stream->id & 0xe0) == AUDIO_ID)
615             /* The VCD standard demands that 20 zero bytes follow
616                each audio packet (see standard p. IV-8).*/
617             buf_index+=20;
618     }
619     return s->packet_size - buf_index;
620 }
621 #endif
622
623 /* Write an MPEG padding packet header. */
624 static void put_padding_packet(AVFormatContext *ctx, AVIOContext *pb,int packet_bytes)
625 {
626     MpegMuxContext *s = ctx->priv_data;
627     int i;
628
629     avio_wb32(pb, PADDING_STREAM);
630     avio_wb16(pb, packet_bytes - 6);
631     if (!s->is_mpeg2) {
632         avio_w8(pb, 0x0f);
633         packet_bytes -= 7;
634     } else
635         packet_bytes -= 6;
636
637     for(i=0;i<packet_bytes;i++)
638         avio_w8(pb, 0xff);
639 }
640
641 static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len){
642     int nb_frames=0;
643     PacketDesc *pkt_desc= stream->premux_packet;
644
645     while(len>0){
646         if(pkt_desc->size == pkt_desc->unwritten_size)
647             nb_frames++;
648         len -= pkt_desc->unwritten_size;
649         pkt_desc= pkt_desc->next;
650     }
651
652     return nb_frames;
653 }
654
655 /* flush the packet on stream stream_index */
656 static int flush_packet(AVFormatContext *ctx, int stream_index,
657                          int64_t pts, int64_t dts, int64_t scr, int trailer_size)
658 {
659     MpegMuxContext *s = ctx->priv_data;
660     StreamInfo *stream = ctx->streams[stream_index]->priv_data;
661     uint8_t *buf_ptr;
662     int size, payload_size, startcode, id, stuffing_size, i, header_len;
663     int packet_size;
664     uint8_t buffer[128];
665     int zero_trail_bytes = 0;
666     int pad_packet_bytes = 0;
667     int pes_flags;
668     int general_pack = 0;  /*"general" pack without data specific to one stream?*/
669     int nb_frames;
670
671     id = stream->id;
672
673     av_dlog(ctx, "packet ID=%2x PTS=%0.3f\n", id, pts / 90000.0);
674
675     buf_ptr = buffer;
676
677     if ((s->packet_number % s->pack_header_freq) == 0 || s->last_scr != scr) {
678         /* output pack and systems header if needed */
679         size = put_pack_header(ctx, buf_ptr, scr);
680         buf_ptr += size;
681         s->last_scr= scr;
682
683         if (s->is_vcd) {
684             /* there is exactly one system header for each stream in a VCD MPEG,
685                One in the very first video packet and one in the very first
686                audio packet (see VCD standard p. IV-7 and IV-8).*/
687
688             if (stream->packet_number==0) {
689                 size = put_system_header(ctx, buf_ptr, id);
690                 buf_ptr += size;
691             }
692         } else if (s->is_dvd) {
693             if (stream->align_iframe || s->packet_number == 0){
694                 int PES_bytes_to_fill = s->packet_size - size - 10;
695
696                 if (pts != AV_NOPTS_VALUE) {
697                     if (dts != pts)
698                         PES_bytes_to_fill -= 5 + 5;
699                     else
700                         PES_bytes_to_fill -= 5;
701                 }
702
703                 if (stream->bytes_to_iframe == 0 || s->packet_number == 0) {
704                     size = put_system_header(ctx, buf_ptr, 0);
705                     buf_ptr += size;
706                     size = buf_ptr - buffer;
707                     avio_write(ctx->pb, buffer, size);
708
709                     avio_wb32(ctx->pb, PRIVATE_STREAM_2);
710                     avio_wb16(ctx->pb, 0x03d4);         // length
711                     avio_w8(ctx->pb, 0x00);           // substream ID, 00=PCI
712                     for (i = 0; i < 979; i++)
713                         avio_w8(ctx->pb, 0x00);
714
715                     avio_wb32(ctx->pb, PRIVATE_STREAM_2);
716                     avio_wb16(ctx->pb, 0x03fa);         // length
717                     avio_w8(ctx->pb, 0x01);           // substream ID, 01=DSI
718                     for (i = 0; i < 1017; i++)
719                         avio_w8(ctx->pb, 0x00);
720
721                     memset(buffer, 0, 128);
722                     buf_ptr = buffer;
723                     s->packet_number++;
724                     stream->align_iframe = 0;
725                     scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
726                     size = put_pack_header(ctx, buf_ptr, scr);
727                     s->last_scr= scr;
728                     buf_ptr += size;
729                     /* GOP Start */
730                 } else if (stream->bytes_to_iframe < PES_bytes_to_fill) {
731                     pad_packet_bytes = PES_bytes_to_fill - stream->bytes_to_iframe;
732                 }
733             }
734         } else {
735             if ((s->packet_number % s->system_header_freq) == 0) {
736                 size = put_system_header(ctx, buf_ptr, 0);
737                 buf_ptr += size;
738             }
739         }
740     }
741     size = buf_ptr - buffer;
742     avio_write(ctx->pb, buffer, size);
743
744     packet_size = s->packet_size - size;
745
746     if (s->is_vcd && (id & 0xe0) == AUDIO_ID)
747         /* The VCD standard demands that 20 zero bytes follow
748            each audio pack (see standard p. IV-8).*/
749         zero_trail_bytes += 20;
750
751     if ((s->is_vcd && stream->packet_number==0)
752         || (s->is_svcd && s->packet_number==0)) {
753         /* for VCD the first pack of each stream contains only the pack header,
754            the system header and lots of padding (see VCD standard p. IV-6).
755            In the case of an audio pack, 20 zero bytes are also added at
756            the end.*/
757         /* For SVCD we fill the very first pack to increase compatibility with
758            some DVD players. Not mandated by the standard.*/
759         if (s->is_svcd)
760             general_pack = 1;    /* the system header refers to both streams and no stream data*/
761         pad_packet_bytes = packet_size - zero_trail_bytes;
762     }
763
764     packet_size -= pad_packet_bytes + zero_trail_bytes;
765
766     if (packet_size > 0) {
767
768         /* packet header size */
769         packet_size -= 6;
770
771         /* packet header */
772         if (s->is_mpeg2) {
773             header_len = 3;
774             if (stream->packet_number==0)
775                 header_len += 3; /* PES extension */
776             header_len += 1; /* obligatory stuffing byte */
777         } else {
778             header_len = 0;
779         }
780         if (pts != AV_NOPTS_VALUE) {
781             if (dts != pts)
782                 header_len += 5 + 5;
783             else
784                 header_len += 5;
785         } else {
786             if (!s->is_mpeg2)
787                 header_len++;
788         }
789
790         payload_size = packet_size - header_len;
791         if (id < 0xc0) {
792             startcode = PRIVATE_STREAM_1;
793             payload_size -= 1;
794             if (id >= 0x40) {
795                 payload_size -= 3;
796                 if (id >= 0xa0)
797                     payload_size -= 3;
798             }
799         } else {
800             startcode = 0x100 + id;
801         }
802
803         stuffing_size = payload_size - av_fifo_size(stream->fifo);
804
805         // first byte does not fit -> reset pts/dts + stuffing
806         if(payload_size <= trailer_size && pts != AV_NOPTS_VALUE){
807             int timestamp_len=0;
808             if(dts != pts)
809                 timestamp_len += 5;
810             if(pts != AV_NOPTS_VALUE)
811                 timestamp_len += s->is_mpeg2 ? 5 : 4;
812             pts=dts= AV_NOPTS_VALUE;
813             header_len -= timestamp_len;
814             if (s->is_dvd && stream->align_iframe) {
815                 pad_packet_bytes += timestamp_len;
816                 packet_size  -= timestamp_len;
817             } else {
818                 payload_size += timestamp_len;
819             }
820             stuffing_size += timestamp_len;
821             if(payload_size > trailer_size)
822                 stuffing_size += payload_size - trailer_size;
823         }
824
825         if (pad_packet_bytes > 0 && pad_packet_bytes <= 7) { // can't use padding, so use stuffing
826             packet_size += pad_packet_bytes;
827             payload_size += pad_packet_bytes; // undo the previous adjustment
828             if (stuffing_size < 0) {
829                 stuffing_size  = pad_packet_bytes;
830             } else {
831                 stuffing_size += pad_packet_bytes;
832             }
833             pad_packet_bytes = 0;
834         }
835
836         if (stuffing_size < 0)
837             stuffing_size = 0;
838
839         if (startcode == PRIVATE_STREAM_1 && id >= 0xa0) {
840             if (payload_size < av_fifo_size(stream->fifo))
841                 stuffing_size += payload_size % stream->lpcm_align;
842         }
843
844         if (stuffing_size > 16) {    /*<=16 for MPEG-1, <=32 for MPEG-2*/
845             pad_packet_bytes += stuffing_size;
846             packet_size      -= stuffing_size;
847             payload_size     -= stuffing_size;
848             stuffing_size = 0;
849         }
850
851         nb_frames= get_nb_frames(ctx, stream, payload_size - stuffing_size);
852
853         avio_wb32(ctx->pb, startcode);
854
855         avio_wb16(ctx->pb, packet_size);
856
857         if (!s->is_mpeg2)
858             for(i=0;i<stuffing_size;i++)
859                 avio_w8(ctx->pb, 0xff);
860
861         if (s->is_mpeg2) {
862             avio_w8(ctx->pb, 0x80); /* mpeg2 id */
863
864             pes_flags=0;
865
866             if (pts != AV_NOPTS_VALUE) {
867                 pes_flags |= 0x80;
868                 if (dts != pts)
869                     pes_flags |= 0x40;
870             }
871
872             /* Both the MPEG-2 and the SVCD standards demand that the
873                P-STD_buffer_size field be included in the first packet of
874                every stream. (see SVCD standard p. 26 V.2.3.1 and V.2.3.2
875                and MPEG-2 standard 2.7.7) */
876             if (stream->packet_number == 0)
877                 pes_flags |= 0x01;
878
879             avio_w8(ctx->pb, pes_flags); /* flags */
880             avio_w8(ctx->pb, header_len - 3 + stuffing_size);
881
882             if (pes_flags & 0x80)  /*write pts*/
883                 put_timestamp(ctx->pb, (pes_flags & 0x40) ? 0x03 : 0x02, pts);
884             if (pes_flags & 0x40)  /*write dts*/
885                 put_timestamp(ctx->pb, 0x01, dts);
886
887             if (pes_flags & 0x01) {  /*write pes extension*/
888                 avio_w8(ctx->pb, 0x10); /* flags */
889
890                 /* P-STD buffer info */
891                 if ((id & 0xe0) == AUDIO_ID)
892                     avio_wb16(ctx->pb, 0x4000 | stream->max_buffer_size/ 128);
893                 else
894                     avio_wb16(ctx->pb, 0x6000 | stream->max_buffer_size/1024);
895             }
896
897         } else {
898             if (pts != AV_NOPTS_VALUE) {
899                 if (dts != pts) {
900                     put_timestamp(ctx->pb, 0x03, pts);
901                     put_timestamp(ctx->pb, 0x01, dts);
902                 } else {
903                     put_timestamp(ctx->pb, 0x02, pts);
904                 }
905             } else {
906                 avio_w8(ctx->pb, 0x0f);
907             }
908         }
909
910         if (s->is_mpeg2) {
911             /* special stuffing byte that is always written
912                to prevent accidental generation of start codes. */
913             avio_w8(ctx->pb, 0xff);
914
915             for(i=0;i<stuffing_size;i++)
916                 avio_w8(ctx->pb, 0xff);
917         }
918
919         if (startcode == PRIVATE_STREAM_1) {
920             avio_w8(ctx->pb, id);
921             if (id >= 0xa0) {
922                 /* LPCM (XXX: check nb_frames) */
923                 avio_w8(ctx->pb, 7);
924                 avio_wb16(ctx->pb, 4); /* skip 3 header bytes */
925                 avio_w8(ctx->pb, stream->lpcm_header[0]);
926                 avio_w8(ctx->pb, stream->lpcm_header[1]);
927                 avio_w8(ctx->pb, stream->lpcm_header[2]);
928             } else if (id >= 0x40) {
929                 /* AC-3 */
930                 avio_w8(ctx->pb, nb_frames);
931                 avio_wb16(ctx->pb, trailer_size+1);
932             }
933         }
934
935         /* output data */
936         assert(payload_size - stuffing_size <= av_fifo_size(stream->fifo));
937         av_fifo_generic_read(stream->fifo, ctx->pb, payload_size - stuffing_size, (void*)avio_write);
938         stream->bytes_to_iframe -= payload_size - stuffing_size;
939     }else{
940         payload_size=
941         stuffing_size= 0;
942     }
943
944     if (pad_packet_bytes > 0)
945         put_padding_packet(ctx,ctx->pb, pad_packet_bytes);
946
947     for(i=0;i<zero_trail_bytes;i++)
948         avio_w8(ctx->pb, 0x00);
949
950     avio_flush(ctx->pb);
951
952     s->packet_number++;
953
954     /* only increase the stream packet number if this pack actually contains
955        something that is specific to this stream! I.e. a dedicated header
956        or some data.*/
957     if (!general_pack)
958         stream->packet_number++;
959
960     return payload_size - stuffing_size;
961 }
962
963 static void put_vcd_padding_sector(AVFormatContext *ctx)
964 {
965     /* There are two ways to do this padding: writing a sector/pack
966        of 0 values, or writing an MPEG padding pack. Both seem to
967        work with most decoders, BUT the VCD standard only allows a 0-sector
968        (see standard p. IV-4, IV-5).
969        So a 0-sector it is...*/
970
971     MpegMuxContext *s = ctx->priv_data;
972     int i;
973
974     for(i=0;i<s->packet_size;i++)
975         avio_w8(ctx->pb, 0);
976
977     s->vcd_padding_bytes_written += s->packet_size;
978
979     avio_flush(ctx->pb);
980
981     /* increasing the packet number is correct. The SCR of the following packs
982        is calculated from the packet_number and it has to include the padding
983        sector (it represents the sector index, not the MPEG pack index)
984        (see VCD standard p. IV-6)*/
985     s->packet_number++;
986 }
987
988 #if 0 /* unused, remove? */
989 static int64_t get_vcd_scr(AVFormatContext *ctx,int stream_index,int64_t pts)
990 {
991     MpegMuxContext *s = ctx->priv_data;
992     int64_t scr;
993
994         /* Since the data delivery rate is constant, SCR is computed
995            using the formula C + i * 1200 where C is the start constant
996            and i is the pack index.
997            It is recommended that SCR 0 is at the beginning of the VCD front
998            margin (a sequence of empty Form 2 sectors on the CD).
999            It is recommended that the front margin is 30 sectors long, so
1000            we use C = 30*1200 = 36000
1001            (Note that even if the front margin is not 30 sectors the file
1002            will still be correct according to the standard. It just won't have
1003            the "recommended" value).*/
1004         scr = 36000 + s->packet_number * 1200;
1005
1006     return scr;
1007 }
1008 #endif
1009
1010 static int remove_decoded_packets(AVFormatContext *ctx, int64_t scr){
1011 //    MpegMuxContext *s = ctx->priv_data;
1012     int i;
1013
1014     for(i=0; i<ctx->nb_streams; i++){
1015         AVStream *st = ctx->streams[i];
1016         StreamInfo *stream = st->priv_data;
1017         PacketDesc *pkt_desc;
1018
1019         while((pkt_desc= stream->predecode_packet)
1020               && scr > pkt_desc->dts){ //FIXME > vs >=
1021             if(stream->buffer_index < pkt_desc->size ||
1022                stream->predecode_packet == stream->premux_packet){
1023                 av_log(ctx, AV_LOG_ERROR,
1024                        "buffer underflow i=%d bufi=%d size=%d\n",
1025                        i, stream->buffer_index, pkt_desc->size);
1026                 break;
1027             }
1028             stream->buffer_index -= pkt_desc->size;
1029
1030             stream->predecode_packet= pkt_desc->next;
1031             av_freep(&pkt_desc);
1032         }
1033     }
1034
1035     return 0;
1036 }
1037
1038 static int output_packet(AVFormatContext *ctx, int flush){
1039     MpegMuxContext *s = ctx->priv_data;
1040     AVStream *st;
1041     StreamInfo *stream;
1042     int i, avail_space=0, es_size, trailer_size;
1043     int best_i= -1;
1044     int best_score= INT_MIN;
1045     int ignore_constraints=0;
1046     int64_t scr= s->last_scr;
1047     PacketDesc *timestamp_packet;
1048     const int64_t max_delay= av_rescale(ctx->max_delay, 90000, AV_TIME_BASE);
1049
1050 retry:
1051     for(i=0; i<ctx->nb_streams; i++){
1052         AVStream *st = ctx->streams[i];
1053         StreamInfo *stream = st->priv_data;
1054         const int avail_data=  av_fifo_size(stream->fifo);
1055         const int space= stream->max_buffer_size - stream->buffer_index;
1056         int rel_space= 1024LL*space / stream->max_buffer_size;
1057         PacketDesc *next_pkt= stream->premux_packet;
1058
1059         /* for subtitle, a single PES packet must be generated,
1060            so we flush after every single subtitle packet */
1061         if(s->packet_size > avail_data && !flush
1062            && st->codec->codec_type != AVMEDIA_TYPE_SUBTITLE)
1063             return 0;
1064         if(avail_data==0)
1065             continue;
1066         assert(avail_data>0);
1067
1068         if(space < s->packet_size && !ignore_constraints)
1069             continue;
1070
1071         if(next_pkt && next_pkt->dts - scr > max_delay)
1072             continue;
1073
1074         if(rel_space > best_score){
1075             best_score= rel_space;
1076             best_i = i;
1077             avail_space= space;
1078         }
1079     }
1080
1081     if(best_i < 0){
1082         int64_t best_dts= INT64_MAX;
1083
1084         for(i=0; i<ctx->nb_streams; i++){
1085             AVStream *st = ctx->streams[i];
1086             StreamInfo *stream = st->priv_data;
1087             PacketDesc *pkt_desc= stream->predecode_packet;
1088             if(pkt_desc && pkt_desc->dts < best_dts)
1089                 best_dts= pkt_desc->dts;
1090         }
1091
1092         av_dlog(ctx, "bumping scr, scr:%f, dts:%f\n",
1093                 scr / 90000.0, best_dts / 90000.0);
1094         if(best_dts == INT64_MAX)
1095             return 0;
1096
1097         if(scr >= best_dts+1 && !ignore_constraints){
1098             av_log(ctx, AV_LOG_ERROR, "packet too large, ignoring buffer limits to mux it\n");
1099             ignore_constraints= 1;
1100         }
1101         scr= FFMAX(best_dts+1, scr);
1102         if(remove_decoded_packets(ctx, scr) < 0)
1103             return -1;
1104         goto retry;
1105     }
1106
1107     assert(best_i >= 0);
1108
1109     st = ctx->streams[best_i];
1110     stream = st->priv_data;
1111
1112     assert(av_fifo_size(stream->fifo) > 0);
1113
1114     assert(avail_space >= s->packet_size || ignore_constraints);
1115
1116     timestamp_packet= stream->premux_packet;
1117     if(timestamp_packet->unwritten_size == timestamp_packet->size){
1118         trailer_size= 0;
1119     }else{
1120         trailer_size= timestamp_packet->unwritten_size;
1121         timestamp_packet= timestamp_packet->next;
1122     }
1123
1124     if(timestamp_packet){
1125 //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);
1126         es_size= flush_packet(ctx, best_i, timestamp_packet->pts, timestamp_packet->dts, scr, trailer_size);
1127     }else{
1128         assert(av_fifo_size(stream->fifo) == trailer_size);
1129         es_size= flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr, trailer_size);
1130     }
1131
1132     if (s->is_vcd) {
1133         /* Write one or more padding sectors, if necessary, to reach
1134            the constant overall bitrate.*/
1135         int vcd_pad_bytes;
1136
1137         while((vcd_pad_bytes = get_vcd_padding_size(ctx,stream->premux_packet->pts) ) >= s->packet_size){ //FIXME pts cannot be correct here
1138             put_vcd_padding_sector(ctx);
1139             s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
1140         }
1141     }
1142
1143     stream->buffer_index += es_size;
1144     s->last_scr += s->packet_size*90000LL / (s->mux_rate*50LL); //FIXME rounding and first few bytes of each packet
1145
1146     while(stream->premux_packet && stream->premux_packet->unwritten_size <= es_size){
1147         es_size -= stream->premux_packet->unwritten_size;
1148         stream->premux_packet= stream->premux_packet->next;
1149     }
1150     if(es_size)
1151         stream->premux_packet->unwritten_size -= es_size;
1152
1153     if(remove_decoded_packets(ctx, s->last_scr) < 0)
1154         return -1;
1155
1156     return 1;
1157 }
1158
1159 static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
1160 {
1161     MpegMuxContext *s = ctx->priv_data;
1162     int stream_index= pkt->stream_index;
1163     int size= pkt->size;
1164     uint8_t *buf= pkt->data;
1165     AVStream *st = ctx->streams[stream_index];
1166     StreamInfo *stream = st->priv_data;
1167     int64_t pts, dts;
1168     PacketDesc *pkt_desc;
1169     int preload;
1170     const int is_iframe = st->codec->codec_type == AVMEDIA_TYPE_VIDEO && (pkt->flags & AV_PKT_FLAG_KEY);
1171
1172     preload = av_rescale(s->preload, 90000, AV_TIME_BASE);
1173
1174     pts= pkt->pts;
1175     dts= pkt->dts;
1176
1177     if(pts != AV_NOPTS_VALUE) pts += 2*preload;
1178     if(dts != AV_NOPTS_VALUE){
1179         if(!s->last_scr)
1180             s->last_scr= dts + preload;
1181         dts += 2*preload;
1182     }
1183
1184 //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);
1185     if (!stream->premux_packet)
1186         stream->next_packet = &stream->premux_packet;
1187     *stream->next_packet=
1188     pkt_desc= av_mallocz(sizeof(PacketDesc));
1189     pkt_desc->pts= pts;
1190     pkt_desc->dts= dts;
1191     pkt_desc->unwritten_size=
1192     pkt_desc->size= size;
1193     if(!stream->predecode_packet)
1194         stream->predecode_packet= pkt_desc;
1195     stream->next_packet= &pkt_desc->next;
1196
1197     if (av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size) < 0)
1198         return -1;
1199
1200     if (s->is_dvd){
1201         if (is_iframe && (s->packet_number == 0 || (pts - stream->vobu_start_pts >= 36000))) { // min VOBU length 0.4 seconds (mpucoder)
1202             stream->bytes_to_iframe = av_fifo_size(stream->fifo);
1203             stream->align_iframe = 1;
1204             stream->vobu_start_pts = pts;
1205         }
1206     }
1207
1208     av_fifo_generic_write(stream->fifo, buf, size, NULL);
1209
1210     for(;;){
1211         int ret= output_packet(ctx, 0);
1212         if(ret<=0)
1213             return ret;
1214     }
1215 }
1216
1217 static int mpeg_mux_end(AVFormatContext *ctx)
1218 {
1219 //    MpegMuxContext *s = ctx->priv_data;
1220     StreamInfo *stream;
1221     int i;
1222
1223     for(;;){
1224         int ret= output_packet(ctx, 1);
1225         if(ret<0)
1226             return ret;
1227         else if(ret==0)
1228             break;
1229     }
1230
1231     /* End header according to MPEG1 systems standard. We do not write
1232        it as it is usually not needed by decoders and because it
1233        complicates MPEG stream concatenation. */
1234     //avio_wb32(ctx->pb, ISO_11172_END_CODE);
1235     //avio_flush(ctx->pb);
1236
1237     for(i=0;i<ctx->nb_streams;i++) {
1238         stream = ctx->streams[i]->priv_data;
1239
1240         assert(av_fifo_size(stream->fifo) == 0);
1241         av_fifo_free(stream->fifo);
1242     }
1243     return 0;
1244 }
1245
1246 #define OFFSET(x) offsetof(MpegMuxContext, x)
1247 #define E AV_OPT_FLAG_ENCODING_PARAM
1248 static const AVOption options[] = {
1249     { "muxrate", NULL, OFFSET(user_mux_rate), AV_OPT_TYPE_INT, {0}, 0, INT_MAX, E },
1250     { "preload", "Initial demux-decode delay in microseconds.", OFFSET(preload),  AV_OPT_TYPE_INT, {500000}, 0, INT_MAX, E},
1251     { NULL },
1252 };
1253
1254 #define MPEGENC_CLASS(flavor)\
1255 static const AVClass flavor ## _class = {\
1256     .class_name = #flavor " muxer",\
1257     .item_name  = av_default_item_name,\
1258     .version    = LIBAVUTIL_VERSION_INT,\
1259     .option     = options,\
1260 };
1261
1262 #if CONFIG_MPEG1SYSTEM_MUXER
1263 MPEGENC_CLASS(mpeg)
1264 AVOutputFormat ff_mpeg1system_muxer = {
1265     .name              = "mpeg",
1266     .long_name         = NULL_IF_CONFIG_SMALL("MPEG-1 System format"),
1267     .mime_type         = "video/mpeg",
1268     .extensions        = "mpg,mpeg",
1269     .priv_data_size    = sizeof(MpegMuxContext),
1270     .audio_codec       = CODEC_ID_MP2,
1271     .video_codec       = CODEC_ID_MPEG1VIDEO,
1272     .write_header      = mpeg_mux_init,
1273     .write_packet      = mpeg_mux_write_packet,
1274     .write_trailer     = mpeg_mux_end,
1275     .priv_class        = &mpeg_class,
1276 };
1277 #endif
1278 #if CONFIG_MPEG1VCD_MUXER
1279 MPEGENC_CLASS(vcd)
1280 AVOutputFormat ff_mpeg1vcd_muxer = {
1281     .name              = "vcd",
1282     .long_name         = NULL_IF_CONFIG_SMALL("MPEG-1 System format (VCD)"),
1283     .mime_type         = "video/mpeg",
1284     .priv_data_size    = sizeof(MpegMuxContext),
1285     .audio_codec       = CODEC_ID_MP2,
1286     .video_codec       = CODEC_ID_MPEG1VIDEO,
1287     .write_header      = mpeg_mux_init,
1288     .write_packet      = mpeg_mux_write_packet,
1289     .write_trailer     = mpeg_mux_end,
1290     .priv_class        = &vcd_class,
1291 };
1292 #endif
1293 #if CONFIG_MPEG2VOB_MUXER
1294 MPEGENC_CLASS(vob)
1295 AVOutputFormat ff_mpeg2vob_muxer = {
1296     .name              = "vob",
1297     .long_name         = NULL_IF_CONFIG_SMALL("MPEG-2 PS format (VOB)"),
1298     .mime_type         = "video/mpeg",
1299     .extensions        = "vob",
1300     .priv_data_size    = sizeof(MpegMuxContext),
1301     .audio_codec       = CODEC_ID_MP2,
1302     .video_codec       = CODEC_ID_MPEG2VIDEO,
1303     .write_header      = mpeg_mux_init,
1304     .write_packet      = mpeg_mux_write_packet,
1305     .write_trailer     = mpeg_mux_end,
1306     .priv_class        = &vob_class,
1307 };
1308 #endif
1309
1310 /* Same as mpeg2vob_mux except that the pack size is 2324 */
1311 #if CONFIG_MPEG2SVCD_MUXER
1312 MPEGENC_CLASS(svcd)
1313 AVOutputFormat ff_mpeg2svcd_muxer = {
1314     .name              = "svcd",
1315     .long_name         = NULL_IF_CONFIG_SMALL("MPEG-2 PS format (VOB)"),
1316     .mime_type         = "video/mpeg",
1317     .extensions        = "vob",
1318     .priv_data_size    = sizeof(MpegMuxContext),
1319     .audio_codec       = CODEC_ID_MP2,
1320     .video_codec       = CODEC_ID_MPEG2VIDEO,
1321     .write_header      = mpeg_mux_init,
1322     .write_packet      = mpeg_mux_write_packet,
1323     .write_trailer     = mpeg_mux_end,
1324     .priv_class        = &svcd_class,
1325 };
1326 #endif
1327
1328 /*  Same as mpeg2vob_mux except the 'is_dvd' flag is set to produce NAV pkts */
1329 #if CONFIG_MPEG2DVD_MUXER
1330 MPEGENC_CLASS(dvd)
1331 AVOutputFormat ff_mpeg2dvd_muxer = {
1332     .name              = "dvd",
1333     .long_name         = NULL_IF_CONFIG_SMALL("MPEG-2 PS format (DVD VOB)"),
1334     .mime_type         = "video/mpeg",
1335     .extensions        = "dvd",
1336     .priv_data_size    = sizeof(MpegMuxContext),
1337     .audio_codec       = CODEC_ID_MP2,
1338     .video_codec       = CODEC_ID_MPEG2VIDEO,
1339     .write_header      = mpeg_mux_init,
1340     .write_packet      = mpeg_mux_write_packet,
1341     .write_trailer     = mpeg_mux_end,
1342     .priv_class        = &dvd_class,
1343 };
1344 #endif