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