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