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