OSDN Git Service

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