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