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