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